sbWatchFolderService.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 sbWatchFolderService_h_
28 #define sbWatchFolderService_h_
29 
30 #include "sbWatchFolderPrefMgr.h"
31 
32 #include <sbIWatchFolderService.h>
33 #include <sbIFileSystemWatcher.h>
34 #include <sbIFileSystemListener.h>
35 #include <sbILibrary.h>
36 #include <sbIJobProgress.h>
37 #include <sbILibraryUtils.h>
38 #include <sbIMediaListListener.h>
39 
40 #include <nsITimer.h>
41 #include <nsIComponentManager.h>
42 #include <nsIGenericFactory.h>
43 #include <nsIFile.h>
44 #include <nsTArray.h>
45 #include <nsIMutableArray.h>
46 #include <nsISupportsPrimitives.h>
47 #include <nsStringAPI.h>
48 #include <nsCOMPtr.h>
49 #include <nsIIOService.h>
50 #include <nsIDOMWindow.h>
51 #include <nsUnicharUtils.h>
52 
53 #include <set>
54 #include <map>
55 
56 
57 //==============================================================================
58 //
59 // @brief Ignore case comparision function for the string maps.
60 //
61 //==============================================================================
62 
64 {
65  bool operator() (const nsAString & s1, const nsAString & s2) const
66  {
67 #if defined(XP_WIN)
68  return s1.Compare(s2, CaseInsensitiveCompare) < 0;
69 #else
70  return s1.Compare(s2) < 0;
71 #endif
72  }
73 };
74 
75 //==============================================================================
76 //
77 // @interface sbWatchFolderService
78 // @brief Songbird watch folder implementation class.
79 //
80 //==============================================================================
81 
83  public sbIFileSystemListener,
85  public nsITimerCallback,
87 {
88  friend class sbWatchFolderPrefMgr;
89 
90 public:
92  virtual ~sbWatchFolderService();
93 
95  NS_DECL_SBIWATCHFOLDERSERVICE
96  NS_DECL_SBIFILESYSTEMLISTENER
97  NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
98  NS_DECL_NSITIMERCALLBACK
99  NS_DECL_SBIJOBPROGRESSLISTENER
100 
101  nsresult Init();
102 
103  static NS_METHOD RegisterSelf(nsIComponentManager* aCompMgr,
104  nsIFile* aPath,
105  const char* aLoaderStr,
106  const char* aType,
107  const nsModuleComponentInfo *aInfo);
108 
109 protected:
110  typedef std::set<nsString, sbStringIgnoreCaseCompare> sbStringSet;
111  typedef sbStringSet::iterator sbStringSetIter;
113  PRInt32 depth; // number of calls to addIgnorePath()
114  PRInt32 count; // number from addIgnoreCount()
116  ignorePathData_t(PRInt32 aDepth, PRInt32 aCount)
117  : depth(aDepth), count(aCount) {}
118  };
119  typedef std::map<nsString, ignorePathData_t, sbStringIgnoreCaseCompare> sbStringMap;
120 
121  typedef enum {
122  eNone = 0,
123  eRemoval = 1,
124  eChanged = 2,
126  } EProcessType;
127 
128  typedef enum {
129  eNotSupported = 0, // Service is not supported on current platform
130  eDisabled = 1, // Service is disabled (by user pref)
131  eStarted = 2, // Service has initialized internally, not watching yet.
132  eWatching = 3, // Service is now watching changes
134 
135 protected:
136  //
137  // \brief Internal delayed startup handling method.
138  //
139  nsresult InitInternal();
140 
141  //
142  // \brief Handles starting up the file system watcher, timers, and other
143  // member resources used to track changes.
144  //
145  nsresult StartWatchingFolder();
146 
147  //
148  // \brief Handle shutting down the file system watcher, timer, and other
149  // member resources that are being used to track changes.
150  //
151  nsresult StopWatchingFolder();
152 
153  //
154  // \brief Set the startup delay timer. This method is used to postpone
155  // watchfolder bootstrapping.
156  //
157  nsresult SetStartupDelayTimer();
158 
159  //
160  // \brief Set the standard event pump timer. This method should be called
161  // everytime a event is received.
162  // NOTE: This method will not init the event pump timer if the
163  // |sbIFileSystemWatcher| has not started watching.
164  //
165  nsresult SetEventPumpTimer();
166 
167  //
168  // \brief This method will handle processing all the normal events (i.e. not
169  // delayed events).
170  //
171  nsresult ProcessEventPaths();
172 
173  //
174  // \brief Handle a set of changed paths for changed and removed items. This
175  // method will look up media items by contentURL.
176  //
177  nsresult HandleEventPathList(sbStringSet & aEventPathSet,
178  EProcessType aProcessType);
179 
180  //
181  // \brief Handle the set of added paths to the library.
182  //
183  nsresult ProcessAddedPaths();
184 
185  //
186  // \brief Enumerate media items in the library with the given paths.
187  //
188  nsresult EnumerateItemsByPaths(sbStringSet & aPathSet);
189 
190  //
191  // \brief Get an array of media item URIs from a list of string paths
192  //
193  nsresult GetURIArrayForStringPaths(sbStringSet & aPathsSet, nsIArray **aURIs);
194 
195  //
196  // \brief Get a |nsIURI| instance for a given absolute path.
197  //
198  nsresult GetFilePathURI(const nsAString & aFilePath, nsIURI **aURIRetVal);
199 
200  //
201  // \brief Get the main Songbird window.
202  //
203  nsresult GetSongbirdWindow(nsIDOMWindow **aSongbirdWindow);
204 
205  //
206  // \brief Handle the situation where the watcher fails to load a saved
207  // session.
208  //
209  nsresult HandleSessionLoadError();
210 
211  //
212  // \brief Handle the situation where the watcher reports that the root watch
213  // folder path is missing.
214  //
215  nsresult HandleRootPathMissing();
216 
217  //
218  // \brief Check to see if a given file path is on the ignored paths list.
219  // If found, decrements the times-to-ignore count as appropriate.
220  //
221  nsresult DecrementIgnoredPathCount(const nsAString & aFilePath,
222  PRBool *aIsIgnoredPath);
223 
224  //----------------------------------------------------------------------------
225  // Pref Changes
226 
227  //
228  // \brief Callback method when the app has started up and the watch folder
229  // service should startup.
230  //
231  nsresult OnAppStartup();
232 
233  //
234  // \brief Callback method when the app is about to shutdown and the watch
235  // folder service should shutdown.
236  //
237  nsresult OnAppShutdown();
238 
239  //
240  // \brief Callback method when the watchfolder path changes.
241  //
242  nsresult OnWatchFolderPathChanged(const nsAString & aNewPath);
243 
244  //
245  // \brief Callback method when the watch folder enable pref has changed.
246  //
247  nsresult OnEnableWatchFolderChanged(PRBool aShouldEnable);
248 
249 private:
250  nsCOMPtr<sbIFileSystemWatcher> mFileSystemWatcher;
251  nsCOMPtr<sbILibrary> mMainLibrary;
252  nsCOMPtr<sbILibraryUtils> mLibraryUtils;
253  nsCOMPtr<nsITimer> mEventPumpTimer;
254  nsCOMPtr<nsITimer> mChangeDelayTimer;
255  nsCOMPtr<nsITimer> mStartupDelayTimer;
256  nsCOMPtr<nsITimer> mFlushFSWatcherTimer;
257  nsCOMPtr<nsIMutableArray> mEnumeratedMediaItems;
258  nsRefPtr<sbWatchFolderPrefMgr> mPrefMgr;
259  sbStringSet mChangedPaths;
260  sbStringSet mDelayedChangedPaths;
261  sbStringSet mAddedPaths;
262  sbStringSet mRemovedPaths;
263  sbStringMap mIgnorePaths;
264  nsString mWatchPath;
265  nsCString mFileSystemWatcherGUID;
266  EWatchFolderState mServiceState;
267  PRBool mHasWatcherStarted;
268  PRBool mShouldReinitWatcher;
269  PRBool mEventPumpTimerIsSet;
270  PRBool mShouldProcessEvents;
271  PRBool mChangeDelayTimerIsSet;
272  EProcessType mCurrentProcessType;
273 };
274 
275 #endif // sbWatchFolderService_h_
_updateCookies aPath
nsresult GetURIArrayForStringPaths(sbStringSet &aPathsSet, nsIArray **aURIs)
std::map< nsString, ignorePathData_t, sbStringIgnoreCaseCompare > sbStringMap
Interface used to enumerate the items in a media list.
std::set< nsString, sbStringIgnoreCaseCompare > sbStringSet
nsresult OnWatchFolderPathChanged(const nsAString &aNewPath)
nsresult OnEnableWatchFolderChanged(PRBool aShouldEnable)
const nsIDOMWindow
nsresult GetSongbirdWindow(nsIDOMWindow **aSongbirdWindow)
nsresult EnumerateItemsByPaths(sbStringSet &aPathSet)
sbStringSet::iterator sbStringSetIter
ignorePathData_t(PRInt32 aDepth, PRInt32 aCount)
nsresult HandleEventPathList(sbStringSet &aEventPathSet, EProcessType aProcessType)
bool operator()(const nsAString &s1, const nsAString &s2) const
std::set< nsString > sbStringSet
Implemented to receive notifications from sbIJobProgress interfaces.
restoreHistoryPrecursor aCount
NS_DECL_ISUPPORTS NS_DECL_SBIWATCHFOLDERSERVICE NS_DECL_SBIFILESYSTEMLISTENER NS_DECL_SBIMEDIALISTENUMERATIONLISTENER NS_DECL_NSITIMERCALLBACK NS_DECL_SBIJOBPROGRESSLISTENER nsresult Init()
nsresult DecrementIgnoredPathCount(const nsAString &aFilePath, PRBool *aIsIgnoredPath)
static NS_METHOD RegisterSelf(nsIComponentManager *aCompMgr, nsIFile *aPath, const char *aLoaderStr, const char *aType, const nsModuleComponentInfo *aInfo)
nsresult GetFilePathURI(const nsAString &aFilePath, nsIURI **aURIRetVal)
nsITimerCallback