Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added copyright and licence header to C++ files and headers for the common directory.
[xestiaab/.git] / source / common / etag.cpp
1 // etag.cpp - ETag subroutines.
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
10 //
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
19 #include "etag.h"
20 #include "../common/dirs.h"
22 ETagDB::~ETagDB(){
23     
24     // Write out the database to file.
25     
26     ProcessLock->try_lock();
27     
28     if (NeedsWrite == TRUE){
29         
30         WriteETagDB();
31         
32     }
33     
34     ProcessLock->unlock();
35     
36 }
38 void ETagDB::SetupDB(wxString AccountDirInc){
39     
40     AccountDir = AccountDirInc;
41     LoadETagDB();
42     
43 }
45 void ETagDB::AddETag(wxString Filename, wxString ETag, wxString ETagOriginal){
46     
47     // Lock or wait if it is already locked.
48     
49     ProcessLock->lock();
50     
51     // Process the ETag addition.
52     
53     FilenameETag.insert(std::make_pair(Filename, ETag));
54     FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
55     
56     NeedsWrite = TRUE;
57     
58     // Unlock.
59     
60     ProcessLock->unlock();
61     
62 }
64 void ETagDB::RemoveETag(wxString Filename){
65     
66     // Lock or wait if it is already locked.
67     
68     ProcessLock->lock();
69     
70     // Remove the ETag.
71     
72     Filename.Trim();
73     
74     FilenameETag.erase(Filename);
75     FilenameETagOriginal.erase(Filename);
76     
77     NeedsWrite = TRUE;
78     
79     // Unlock.
80     
81     ProcessLock->unlock();
82     
83 }
85 void ETagDB::UpdateETag(wxString Filename, wxString ETag){
86     
87     // Lock or wait if it is already locked.
88     
89     ProcessLock->lock();
90     
91     // Update the ETag.
92     
93     FilenameETag.erase(Filename);
94     FilenameETag.insert(std::make_pair(Filename, ETag));
95     
96     NeedsWrite = TRUE;
97     
98     // Unlock.
99     
100     ProcessLock->unlock();
101     
104 void ETagDB::UpdateETag(wxString Filename, wxString ETag, wxString ETagOriginal){
105     
106     // Lock or wait if it is already locked.
107     
108     ProcessLock->lock();
109     
110     // Update the ETag.
111     
112     FilenameETag.erase(Filename);
113     FilenameETagOriginal.erase(Filename);
114     FilenameETag.insert(std::make_pair(Filename, ETag));
115     FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
116     
117     std::map<wxString,wxString>::iterator IterWxS;
118     IterWxS = FilenameETagOriginal.find(Filename);
119     NeedsWrite = TRUE;
120     
121     // Unlock.
122     
123     ProcessLock->unlock();
124     
127 bool ETagDB::ETagExists(wxString Filename){
129     // Lock or wait if it is already locked.
130     
131     ProcessLock->lock();
132     
133     bool FileResult = FALSE;
134     
135     if (FilenameETag.find(Filename) != FilenameETag.end()){
136         
137         FileResult = TRUE;
138            
139     } else {
140     
141         FileResult = FALSE;
142     
143     }
144     
145     // Unlock.
147     ProcessLock->unlock();
148     
149     return FileResult;
153 std::map<wxString,wxString>* ETagDB::GetFilenamePointer(){
154     
155     return &FilenameETag;
156     
159 wxString ETagDB::GetETag(wxString Filename){
160     
161     // Lock or wait if it is already locked.
162     
163     wxString ETagOut;
164     std::map<wxString,wxString>::iterator IterwxS;
165     
166     ProcessLock->lock();
167     
168     // Get the ETag.
169     
170     //IterwxS = FilenameETag.find(Filename);
171     
172     if (FilenameETag.find(Filename) != FilenameETag.end()){
173         
174         IterwxS = FilenameETag.find(Filename);
175         
176         ETagOut = IterwxS->second;
177         
178     }
179     
180     // Unlock.
181     
182     ProcessLock->unlock();
183     
184     return ETagOut;
185     
188 wxString ETagDB::GetETagOriginal(wxString Filename){
189     
190     // Lock or wait if it is already locked.
191     
192     wxString ETagOrigOut;
193     std::map<wxString,wxString>::iterator IterwxS;
194     
195     ProcessLock->lock();
196     
197     // Get the ETag.
198     
199     //IterwxS = FilenameETag.find(Filename);
200     
201     if (FilenameETagOriginal.find(Filename) != FilenameETagOriginal.end()){
202         
203         IterwxS = FilenameETagOriginal.find(Filename);
204         
205         ETagOrigOut = IterwxS->second;
206         
207     }
208     
209     // Unlock.
210     
211     ProcessLock->unlock();
212     
213     return ETagOrigOut;
214     
217 bool ETagDB::WriteETagDB(){
218     
219     // Lock or wait if it is already locked.
220     
221     ProcessLock->lock();
222     
223     // Write the ETag database.
224     
225     wxString AccountETagDB;
226     std::map<wxString,wxString>::iterator IterwxS;
227     int LineSeek = 0;
228     
229     
230     
231 #if defined(__HAIKU__)
232     
233     //preffilename = wxT("noo");
234     
235 #elif defined(__WIN32__)
236     
237     AccountETagDB.Clear();
238     AccountETagDB = GetAccountDir(AccountDir, FALSE);
239     AccountETagDB.Append(wxT("\\etag.db"));
240     
241 #else
242     
243     // This entry applies to OSX as well.
244     
245     AccountETagDB.Clear();
246     AccountETagDB = GetAccountDir(AccountDir, FALSE);
247     AccountETagDB.Append(wxT("/etag.db"));
248     
249 #endif
250     
251     wxTextFile ETagDBFile;
252     wxString SettingLine;
253     
254     if (wxFileExists(AccountETagDB) == FALSE){
255         
256         if (ETagDBFile.Create(AccountETagDB) == FALSE){
257             ////ProcessLock.unlock();
258             return FALSE;
259         }
260         
261     } else {
262         
263         if (ETagDBFile.Open(AccountETagDB) == FALSE){
264             ////ProcessLock.unlock();
265             return FALSE;
266         }
267         
268     }
269     
270     ETagDBFile.Clear();
271     
272     for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
273          iter != FilenameETag.end(); ++iter){
274         
275         // Get Original ETag as well.
276         
277         IterwxS = FilenameETagOriginal.find(iter->first);
278         
279         if (iter->first.IsEmpty()){
280             continue;
281         }
282         
283         SettingLine = iter->first + wxT("|") + iter->second + wxT("|") + IterwxS->second;
284         
285         ETagDBFile.InsertLine(SettingLine, LineSeek);
286         
287         LineSeek++;
288         
289     }
290     
291     ETagDBFile.Write();
292     
293     NeedsWrite = FALSE;
294     
295     // Unlock.
296     
297     ProcessLock->unlock();
298     
299     return TRUE;
300     
303 bool ETagDB::LoadETagDB(){
304     
305     // Lock or wait if it is already locked.
306     
307     ProcessLock->lock();
308     
309     // Load the ETag database.
310     
311     wxString AccountETagDB;
312     wxString AccountDirPath;
313     
314 #if defined(__HAIKU__)
315     
316     //preffilename = wxT("noo");
317     
318 #elif defined(__WIN32__)
319     
320     AccountDirPath = GetAccountDir(AccountDir, FALSE);
321     
322     AccountETagDB = AccountDirPath;
323     AccountETagDB.Append(wxT("\\etag.db"));
324     
325 #else
326     
327     AccountDirPath = GetAccountDir(AccountDir, FALSE);
328     
329     AccountETagDB = AccountDirPath;
330     AccountETagDB.Append(wxT("/etag.db"));
331     
332 #endif
333     
334     wxTextFile ETagDBFile;
335     
336     if (wxFileExists(AccountETagDB) == FALSE){
337         
338         if (ETagDBFile.Create(AccountETagDB) == FALSE){
339             ////ProcessLock.unlock();
340             //ProcessLock->unlock();
341             return FALSE;
342         }
343         
344     } else {
345         
346         if (ETagDBFile.Open(AccountETagDB, wxConvUTF8) == FALSE){
347             ////ProcessLock.unlock();
348             //ProcessLock->unlock();
349             return FALSE;
350         }
351         
352     }
353     
354     wxString wxSFilename;
355     wxString wxSValue;
356     wxString ETagContactFilename;
357     
358     for (wxStringTokenizer ETagDBLine(ETagDBFile.GetFirstLine(), wxT("|"));
359          !ETagDBFile.Eof(); ETagDBLine.SetString(ETagDBFile.GetNextLine(), wxT("|"), wxTOKEN_DEFAULT) )
360     {
361         
362         // Split the file into three.
363         
364         wxSFilename = ETagDBLine.GetNextToken();
365         
366         // Get the ETag.
367         
368         wxSValue = ETagDBLine.GetNextToken();
369         FilenameETag.insert(std::make_pair(wxSFilename, wxSValue));
370         
371         // Get the original ETag.
372         
373         wxSValue = ETagDBLine.GetNextToken();
374         wxSValue.Trim();
375         FilenameETagOriginal.insert(std::make_pair(wxSFilename, wxSValue));
376         
377     }
378     
379     // Unlock.
380     
381     ProcessLock->unlock();
382     
383     return TRUE;
384     
387 void ETagDB::DeleteETagDB(){
388     
389     // Lock or wait if it is already locked.
390     
391     ProcessLock->lock();
392     
393     wxString AccountETagDB;
394     
395     // Delete the ETagDB.
396     
397 #if defined(__HAIKU__)
398     
399     //preffilename = wxT("noo");
400     
401 #elif defined(__WIN32__)
402     
403     AccountETagDB.Clear();
404     AccountETagDB = GetAccountDir(AccountDir, FALSE);
405     AccountETagDB.Append(wxT("\\etag.db"));
406     
407     wxRemoveFile(AccountETagDB);
408     
409 #else
410     
411     AccountETagDB.Clear();
412     AccountETagDB = GetAccountDir(AccountDir, FALSE);
413     AccountETagDB.Append(wxT("/etag.db"));
414     
415     wxRemoveFile(AccountETagDB);
416     
417 #endif
418     
419     // Unlock.
420     
421     ProcessLock->unlock();
422     
425 bool ETagDB::CheckETagExists(wxString Filename){
426     
427     if (FilenameETag.find(Filename) == FilenameETag.end()){
428         
429         return FALSE;
430         
431     } else {
432         
433         return TRUE;
434         
435     }   
436     
439 std::map<wxString,ETagData> ETagDB::GetETagData(){
440     
441     // Lock the database.
442     
443     ProcessLock->lock();
444     
445     // Go through each of the ETags and process them accordingly.
446     
447     std::map<wxString,ETagData> DataSet;
448     
449     for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin(); 
450          iter != FilenameETag.end(); ++iter){
451         
452         std::map<wxString,wxString>::iterator origiter = FilenameETagOriginal.find(iter->first);
453         
454         ETagData etd;
455         
456         etd.ETagValue = iter->second;
457         etd.ETagOrigValue = origiter->second;
458         
459         DataSet.insert(std::make_pair(iter->first, etd));
460         
461     }
462     
463     // Unlock the database.
464     
465     ProcessLock->unlock();
466     
467     return DataSet;
468     
471 bool ETagDB::GetWriteStatus(){
472     
473     return NeedsWrite;
474     
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy