sbMediaFileManager.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-2008 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 // Songbird includes
28 #include <sbIMediaFileManager.h>
29 #include <sbIPropertyManager.h>
30 #include <sbIWatchFolderService.h>
31 
32 // Mozilla includes
33 #include <nsBaseHashtable.h>
34 #include <nsHashKeys.h>
35 #include <nsIFile.h>
36 #include <nsIPrefBranch.h>
37 #include <nsStringGlue.h>
38 #include <nsTArray.h>
39 #include <nsCOMPtr.h>
40 #include <nsNetUtil.h>
41 
42 #define SB_MEDIAFILEMANAGER_DESCRIPTION \
43  "Songbird Media File Manager Implementation"
44 
45 #define SB_MEDIAFILEMANAGER_CID \
46  { \
47  0x7b901232, \
48  0x1dd2, \
49  0x11b2, \
50  {0x8d, 0x6a, 0xe1, 0x78, 0x4d, 0xbd, 0x2d, 0x89} \
51  }
52 
53 // Preference keys
54 #define PREF_MFM_ROOT "songbird.media_management.library."
55 #define PREF_MFM_FILEFORMAT "format.file"
56 #define PREF_MFM_DEFPROPERTY "default.property."
57 #define PREF_MFM_PADTRACKNUM "pad_track_num"
58 
59 // String keys
60 #define STRING_MFM_UNKNOWNPROP "mediamanager.nonexistingproperty"
61 #define STRING_MFM_UNKNOWNPROP_EMPTY "mediamanager.nonexistingproperty.empty"
62 #define STRING_MFM_RECORDINGS_FOLDER "mediamanager.recordings_dir"
63 
65 public:
67  NS_METHOD Init();
68 
70  NS_DECL_SBIMEDIAFILEMANAGER
71 
72 protected:
73  virtual ~sbMediaFileManager();
74 
75  nsresult GetNewPath(sbIMediaItem *aMediaItem,
76  nsString &aPath,
77  PRBool *aRetVal);
78 
79  nsresult GetNewFilename(sbIMediaItem *aMediaItem,
80  nsIURI *aItemUri,
81  nsString &aFilename,
82  PRBool *aRetVal);
83 
84  nsresult CopyRename(sbIMediaItem *aMediaItem,
85  nsIFile *aItemFile,
86  nsIFile *aNewFile,
87  PRBool *aRetVal);
88 
89  nsresult Delete(nsIFile *aItemFile,
90  PRBool *aRetVal);
91 
92  nsresult CheckDirectoryForDeletion(nsIFile *aItemFile);
93 
94  nsresult NormalizeDir(nsString &aDir);
95 
96  nsresult ZeroPadTrackNumber(const nsAString & aTrackNumStr,
97  const nsAString & aTotalTrackCountStr,
98  nsString & aOutString);
99 
100 private:
101  typedef nsTArray<nsString> NameTemplate;
102  typedef nsBaseHashtable<nsStringHashKey,
103  NameTemplate,
104  NameTemplate> NameTemplateMap;
105  typedef nsBaseHashtable<nsStringHashKey,
106  nsCOMPtr<nsIFile>,
107  nsIFile *> MediaFoldersMap;
108 
109  nsresult InitMediaFoldersMap(nsIPropertyBag2 * aProperties);
110 
111  nsresult GetMediaFolder(sbIMediaItem * aMediaItem,
112  nsIFile ** aFolder);
113 
114  nsresult GetMediaFolder(nsIFile * aFile,
115  nsIFile ** aFolder);
116 
117  nsresult InitFolderNameTemplates(nsIPropertyBag2 * aProperties);
118 
119  nsresult GetFolderNameTemplate(sbIMediaItem * aMediaItem,
120  NameTemplate & aNameTemplate);
121 
122  nsresult CheckDirectoryForDeletion_Recursive(nsIFile *aDir);
123 
124  void RemoveBadCharacters(nsString& aStringToParse);
125 
126  nsresult GetUnknownValue(nsString aPropertyKey,
127  nsString& aUnknownValue);
128 
129  nsresult GetFormattedFileFolder(const NameTemplate & aNameTemplate,
130  sbIMediaItem* aMediaItem,
131  PRBool aAppendProperty,
132  PRBool aTrimAtEnd,
133  nsString aFileExtension,
134  nsString& aRetVal);
135 
142  nsresult CheckManagementFolder(sbIMediaItem * aMediaItem);
143 
144  // Hold on to the services we use very often
145  nsCOMPtr<nsIPrefBranch> mPrefBranch;
146  nsCOMPtr<nsINetUtil> mNetUtil;
147  nsCOMPtr<sbIPropertyManager> mPropertyManager;
148  nsCOMPtr<sbIWatchFolderService> mWatchFolderService;
149 
150  // Where our media folder is located.
151  MediaFoldersMap mMediaFolders;
152 
153  // Formating properties (filename, folders, separators)
154  NameTemplate mTrackNameTemplate;
155  NameTemplateMap mFolderNameTemplates;
156 
157  PRBool mInitialized;
158 };
159 
nsresult CheckDirectoryForDeletion(nsIFile *aItemFile)
Checks whether a directory and its parent directories need to be deleted.
virtual ~sbMediaFileManager()
Destructor of the sbMediaFileManager component.
nsresult Delete(nsIFile *aItemFile, PRBool *aRetVal)
Deletes a file associated with an item if it is located in the managed folder.
_updateCookies aPath
nsresult GetNewPath(sbIMediaItem *aMediaItem, nsString &aPath, PRBool *aRetVal)
Construct the new path based on user settings.
sbMediaFileManager()
Constructor of the sbMediaFileManager component.
nsresult NormalizeDir(nsString &aDir)
Makes sure that a directory ends with the path separator if it can.
NS_METHOD Init()
Initialize the sbMediaFileManager component.
nsresult CopyRename(sbIMediaItem *aMediaItem, nsIFile *aItemFile, nsIFile *aNewFile, PRBool *aRetVal)
Copys a file from the old location to the new location, ensures that the destination is in the manage...
nsresult ZeroPadTrackNumber(const nsAString &aTrackNumStr, const nsAString &aTotalTrackCountStr, nsString &aOutString)
Format a track number to pad for zeros against the total track count.
nsresult GetNewFilename(sbIMediaItem *aMediaItem, nsIURI *aItemUri, nsString &aFilename, PRBool *aRetVal)
Construct the new filename based on user settings.
Interface that defines a single item of media in the system.