sbWatchFolder.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef sbWatchFolder_h_
28 #define sbWatchFolder_h_
29 
30 #include <sbIWatchFolder.h>
31 #include <sbIFileSystemWatcher.h>
32 #include <sbIFileSystemListener.h>
33 #include <sbILibrary.h>
34 #include <sbIJobProgress.h>
35 #include <sbILibraryUtils.h>
36 #include <sbIMediaListListener.h>
37 
38 #include <nsITimer.h>
39 #include <nsIComponentManager.h>
40 #include <nsIGenericFactory.h>
41 #include <nsIFile.h>
42 #include <nsTArray.h>
43 #include <nsIMutableArray.h>
44 #include <nsISupportsPrimitives.h>
45 #include <nsStringAPI.h>
46 #include <nsCOMPtr.h>
47 #include <nsIIOService.h>
48 #include <nsIDOMWindow.h>
49 #include <nsUnicharUtils.h>
50 
51 #include <set>
52 #include <map>
53 
54 
55 #define SB_WATCHFOLDER_CLASSNAME \
56  "sbWatchFolder"
57 #define SB_WATCHFOLDER_CID \
58  {0x11ef8123, 0x666b, 0x4acc, {0xab, 0x17, 0x58, 0x7a, 0x5c, 0x44, 0xc5, 0xb9}}
59  // {11EF8123-666B-4acc-AB17-587A5C44C5B9}
60 #define SB_WATCHFOLDER_DESCRIPTION \
61  "Songbird Watch Folder"
62 
63 
64 //==============================================================================
65 //
66 // @interface sbWatchFolder
67 // @brief Songbird watch folder implementation class.
68 //
69 //==============================================================================
70 
72  public sbIFileSystemListener,
74  public nsITimerCallback,
76 {
77 public:
78  sbWatchFolder();
79  virtual ~sbWatchFolder();
80 
82  NS_DECL_SBIWATCHFOLDER
83  NS_DECL_SBIFILESYSTEMLISTENER
84  NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
85  NS_DECL_NSITIMERCALLBACK
86  NS_DECL_SBIJOBPROGRESSLISTENER
87 
88  nsresult Init();
89 
90 protected:
91  struct IgnoringCase
92  {
93  bool operator() (const nsAString & s1, const nsAString & s2) const
94  {
95  #if defined(XP_WIN)
96  return s1.Compare(s2, CaseInsensitiveCompare) < 0;
97  #else
98  return s1.Compare(s2) < 0;
99  #endif
100  }
101  };
102 
103  typedef std::set<nsString, IgnoringCase> sbStringSet;
104  typedef sbStringSet::iterator sbStringSetIter;
106  PRInt32 depth; // number of calls to addIgnorePath()
107  PRInt32 count; // number from addIgnoreCount()
109  ignorePathData_t(PRInt32 aDepth, PRInt32 aCount)
110  : depth(aDepth), count(aCount) {}
111  };
112  typedef std::map<nsString, ignorePathData_t, IgnoringCase> sbStringMap;
113 
114  typedef enum {
115  eNone = 0,
116  eRemoval = 1,
117  eChanged = 2,
119  } EProcessType;
120 
121  typedef enum {
122  eNotSupported = 0, // Service is not supported on current platform
123  eDisabled = 1, // Service is disabled (by user pref)
124  eStarted = 2, // Service has initialized internally, not watching yet.
125  eWatching = 3, // Service is now watching changes
127 
128 protected:
129  //
130  // \brief Internal delayed startup handling method.
131  //
132  nsresult InitInternal();
133 
134  //
135  // \brief Handles starting up the file system watcher, timers, and other
136  // member resources used to track changes.
137  //
138  nsresult StartWatchingFolder();
139 
140  //
141  // \brief Handle shutting down the file system watcher, timer, and other
142  // member resources that are being used to track changes.
143  //
144  nsresult StopWatchingFolder();
145 
146  //
147  // \brief Set the startup delay timer. This method is used to postpone
148  // watchfolder bootstrapping.
149  //
150  nsresult SetStartupDelayTimer();
151 
152  //
153  // \brief Set the standard event pump timer. This method should be called
154  // everytime a event is received.
155  // NOTE: This method will not init the event pump timer if the
156  // |sbIFileSystemWatcher| has not started watching.
157  //
158  nsresult SetEventPumpTimer();
159 
160  //
161  // \brief This method will handle processing all the normal events (i.e. not
162  // delayed events).
163  //
164  nsresult ProcessEventPaths();
165 
166  //
167  // \brief Handle a set of changed paths for changed and removed items. This
168  // method will look up media items by contentURL.
169  //
170  nsresult HandleEventPathList(sbStringSet & aEventPathSet,
171  EProcessType aProcessType);
172 
173  //
174  // \brief Handle the set of added paths to the library.
175  //
176  nsresult ProcessAddedPaths();
177 
178  //
179  // \brief Enumerate media items in the library with the given paths.
180  //
181  nsresult EnumerateItemsByPaths(sbStringSet & aPathSet);
182 
183  //
184  // \brief Get an array of media item URIs from a list of string paths
185  //
186  nsresult GetURIArrayForStringPaths(sbStringSet & aPathsSet, nsIArray **aURIs);
187 
188  //
189  // \brief Get a |nsIURI| instance for a given absolute path.
190  //
191  nsresult GetFilePathURI(const nsAString & aFilePath, nsIURI **aURIRetVal);
192 
193  //
194  // \brief Get the main Songbird window.
195  //
196  nsresult GetSongbirdWindow(nsIDOMWindow **aSongbirdWindow);
197 
198  //
199  // \brief Handle the situation where the watcher fails to load a saved
200  // session.
201  //
202  nsresult HandleSessionLoadError();
203 
204  nsresult Rescan();
205 
206  //
207  // \brief Handle the situation where the watcher reports that the root watch
208  // folder path is missing.
209  //
210  nsresult HandleRootPathMissing();
211 
212  //
213  // \brief Check to see if a given file path is on the ignored paths list.
214  // If found, decrements the times-to-ignore count as appropriate.
215  //
216  nsresult DecrementIgnoredPathCount(const nsAString & aFilePath,
217  PRBool *aIsIgnoredPath);
218 
219 private:
220  nsCOMPtr<sbIFileSystemWatcher> mFileSystemWatcher;
221  nsCOMPtr<sbIMediaList> mMediaList;
222  nsCOMPtr<sbILibraryUtils> mLibraryUtils;
223  nsCOMPtr<nsITimer> mEventPumpTimer;
224  nsCOMPtr<nsITimer> mChangeDelayTimer;
225  nsCOMPtr<nsITimer> mStartupDelayTimer;
226  nsCOMPtr<nsITimer> mFlushFSWatcherTimer;
227  nsCOMPtr<nsIMutableArray> mEnumeratedMediaItems;
228  sbStringSet mChangedPaths;
229  sbStringSet mDelayedChangedPaths;
230  sbStringSet mAddedPaths;
231  sbStringSet mRemovedPaths;
232  sbStringMap mIgnorePaths;
233  nsString mWatchPath;
234  nsCString mFileSystemWatcherGUID;
235  EWatchFolderState mServiceState;
236  PRBool mHasWatcherStarted;
237  PRBool mShouldReinitWatcher;
238  PRBool mEventPumpTimerIsSet;
239  PRBool mShouldProcessEvents;
240  PRBool mChangeDelayTimerIsSet;
241  EProcessType mCurrentProcessType;
242 
243  PRBool mCanInteract;
244  PRBool mShouldSynchronize;
245  nsCOMPtr<sbIDirectoryImportService> mCustomImporter;
246  nsCOMPtr<sbIMediacoreTypeSniffer> mTypeSniffer;
247  nsCOMPtr<sbIFileMetadataService> mMetadataScanner;
248 
249  nsresult Enable();
250  nsresult Disable();
251 };
252 
253 #define SB_WATCHFOLDER_CLASSNAME \
254  "sbWatchFolder"
255 #define SB_WATCHFOLDER_CID \
256  {0x11ef8123, 0x666b, 0x4acc, {0xab, 0x17, 0x58, 0x7a, 0x5c, 0x44, 0xc5, 0xb9}}
257  // {11EF8123-666B-4acc-AB17-587A5C44C5B9}
258 #define SB_WATCHFOLDER_DESCRIPTION \
259  "Songbird Watch Folder"
260 
261 #endif // sbWatchFolder_h_
sbStringSet::iterator sbStringSetIter
nsresult StartWatchingFolder()
bool operator()(const nsAString &s1, const nsAString &s2) const
Definition: sbWatchFolder.h:93
nsresult Rescan()
Interface used to enumerate the items in a media list.
nsresult HandleSessionLoadError()
virtual ~sbWatchFolder()
nsresult GetSongbirdWindow(nsIDOMWindow **aSongbirdWindow)
nsresult InitInternal()
const nsIDOMWindow
NS_DECL_ISUPPORTS NS_DECL_SBIWATCHFOLDER NS_DECL_SBIFILESYSTEMLISTENER NS_DECL_SBIMEDIALISTENUMERATIONLISTENER NS_DECL_NSITIMERCALLBACK NS_DECL_SBIJOBPROGRESSLISTENER nsresult Init()
nsresult StopWatchingFolder()
nsresult ProcessEventPaths()
nsresult GetFilePathURI(const nsAString &aFilePath, nsIURI **aURIRetVal)
std::map< nsString, ignorePathData_t, IgnoringCase > sbStringMap
nsresult GetURIArrayForStringPaths(sbStringSet &aPathsSet, nsIArray **aURIs)
nsresult ProcessAddedPaths()
nsresult EnumerateItemsByPaths(sbStringSet &aPathSet)
nsresult DecrementIgnoredPathCount(const nsAString &aFilePath, PRBool *aIsIgnoredPath)
std::set< nsString > sbStringSet
nsresult SetEventPumpTimer()
nsresult HandleEventPathList(sbStringSet &aEventPathSet, EProcessType aProcessType)
Implemented to receive notifications from sbIJobProgress interfaces.
nsresult HandleRootPathMissing()
ignorePathData_t(PRInt32 aDepth, PRInt32 aCount)
restoreHistoryPrecursor aCount
nsITimerCallback
nsresult SetStartupDelayTimer()
std::set< nsString, IgnoringCase > sbStringSet