Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 178 lines (134 sloc) 4.026 kb
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
1 /*
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
2 File: GLibraryManager.m
3 Description: The Goonj library manager (implementation).
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
4
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
5 This file is part of Goonj.
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
6
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
7 Goonj is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
11
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
12 Goonj is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
16
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
17 You should have received a copy of the GNU General Public License
18 along with Goonj. If not, see <http://www.gnu.org/licenses/>.
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
19
20 Copyright 2009 Pratul Kalia.
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
21 Copyright 2009 Ankur Sethi.
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
22 */
23
24 #import "GLibraryManager.h"
4bb096c Added a few new convenience methods to GUtilities. Some new code in G…
GeneralMaximus authored
25 #import "GUtilities.h"
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
26
27
28 @implementation GLibraryManager
29
4bb096c Added a few new convenience methods to GUtilities. Some new code in G…
GeneralMaximus authored
30 - (id) initWithDefaultDatabase
31 {
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
32 if (self = [super init]) {
33 databasePath = [[GUtilities tracksDatabasePath] stringByExpandingTildeInPath];
34 return self;
4bb096c Added a few new convenience methods to GUtilities. Some new code in G…
GeneralMaximus authored
35 }
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
36
4bb096c Added a few new convenience methods to GUtilities. Some new code in G…
GeneralMaximus authored
37 return nil;
38 }
39
40 - (BOOL) createInitialDatabase
41 {
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
42 int err;
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
43 err = sqlite3_open_v2([databasePath cStringUsingEncoding:NSUTF8StringEncoding],
4bb096c Added a few new convenience methods to GUtilities. Some new code in G…
GeneralMaximus authored
44 &databaseConnection,
45 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
46 NULL);
47
48 if (err != SQLITE_OK)
49 return NO;
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
50
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
51 [self createDatabaseSchema];
16f73d0 metadataFromFile is a class method of GTrack.
GeneralMaximus authored
52 [self performInitialScan];
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
53
3c79357 Start the library manager in a new thread from applicationFinishedLau…
GeneralMaximus authored
54 return YES;
55 }
56
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
57 - (BOOL) createDatabaseSchema
58 {
59 NSString *theStatement;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
60
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
61 theStatement = @"CREATE TABLE artists (\
62 id INTEGER PRIMARY KEY,\
63 name TEXT);";
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
64
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
65 if (![self singleStepQuery:theStatement])
66 return NO;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
67
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
68 theStatement = @"CREATE TABLE albums (\
69 id INTEGER PRIMARY KEY,\
70 name TEXT);";
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
71
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
72 if (![self singleStepQuery:theStatement])
73 return NO;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
74
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
75 theStatement = @"CREATE TABLE genres (\
76 id INTEGER PRIMARY KEY,\
77 name TEXT);";
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
78
79
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
80 if (![self singleStepQuery:theStatement])
81 return NO;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
82
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
83 // Should year be text?
84 theStatement = @"CREATE TABLE tracks (\
85 id INTEGER,\
86 path TEXT,\
87 title TEXT,\
88 year TEXT,\
89 rating INTEGER,\
90 artist_id INTEGER,\
91 album_id INTEGER,\
92 genre_id INTEGER,\
93 FOREIGN KEY (artist_id) REFERENCES artists(id),\
94 FOREIGN KEY (album_id) REFERENCES albums(id),\
95 FOREIGN KEY (genre_id) REFERENCES genres(id));";
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
96
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
97 if (![self singleStepQuery:theStatement])
98 return NO;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
99
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
100 return YES;
101 }
102
16f73d0 metadataFromFile is a class method of GTrack.
GeneralMaximus authored
103 - (BOOL) performInitialScan
104 {
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
105 NSArray *watchDirectories = [[NSUserDefaults standardUserDefaults]
106 arrayForKey:@"LibraryFolderLocations"];
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
107
108
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
109 for (NSString *directory in watchDirectories)
110 [self addTracksFromDirectory:[directory stringByExpandingTildeInPath]];
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
111
16f73d0 metadataFromFile is a class method of GTrack.
GeneralMaximus authored
112 return YES;
113 }
114
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
115 - (void) addTracksFromDirectory:(NSString *)aDirectory
116 {
117 NSDirectoryEnumerator *dirEnum = [[NSFileManager defaultManager]
118 enumeratorAtPath:aDirectory];
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
119
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
120 NSString *fileName, *filePath;
121 BOOL isDirectory;
122 while (fileName = [dirEnum nextObject]) {
123 filePath = [aDirectory stringByAppendingPathComponent:fileName];
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
124
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
125 if ([GUtilities isHidden:filePath])
126 continue;
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
127
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
128 [[NSFileManager defaultManager] fileExistsAtPath:filePath
129 isDirectory:&isDirectory];
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
130
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
131 if (isDirectory == YES)
132 [self addTracksFromDirectory:filePath];
133 else
134 [self addTrack:filePath];
135 }
136 }
137
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
138 - (void) addTrack:(NSString *)aURL
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
139 {
edfef1f @lut4rp Reduced the header size to give the playlist table more space. Update…
lut4rp authored
140 // NSLog(@"%@", [GTrack metadataForFile:aURL]);
e99236e Added code for scanning watch directories for the first time.
GeneralMaximus authored
141 }
142
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
143 - (BOOL) singleStepQuery:(NSString *)aQueryString
144 {
145 sqlite3_stmt *preparedStatement;
146 int err;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
147
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
148 err = sqlite3_prepare_v2(databaseConnection,
149 [aQueryString cStringUsingEncoding:NSUTF8StringEncoding],
150 [aQueryString length],
151 &preparedStatement,
152 NULL);
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
153
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
154 if (err == SQLITE_OK)
155 err = sqlite3_step(preparedStatement);
156 else
157 return NO;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
158
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
159 if (err == SQLITE_DONE)
160 sqlite3_finalize(preparedStatement);
161 else
162 return NO;
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
163
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
164 return YES;
165 }
166
3c79357 Start the library manager in a new thread from applicationFinishedLau…
GeneralMaximus authored
167 - (void) startManager
168 {
52805f2 Even more LibraryManager changes. Creating the initial database schem…
GeneralMaximus authored
169 if ([[NSFileManager defaultManager] fileExistsAtPath:databasePath]);
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
170 else
171 [self createInitialDatabase];
3509e3d @lut4rp Fixed tab hell in license headers everywhere. Emacs ftw!
lut4rp authored
172
52805f2 Even more LibraryManager changes. Creating the initial database schem…
GeneralMaximus authored
173 // TODO: remove this once the rest of the manager works.
54b2119 More library manager additions. Non-working, of course :/
GeneralMaximus authored
174 sqlite3_close(databaseConnection);
4bb096c Added a few new convenience methods to GUtilities. Some new code in G…
GeneralMaximus authored
175 }
176
c35abd3 Added the GLibraryManager class.
GeneralMaximus authored
177 @end
Something went wrong with that request. Please try again.