Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
3a9b3f3b3ac1daf844e00c2b02e1cc6843cca1d4
[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         // Setup the ETag Database.
41         
42         AccountDir = AccountDirInc;
43         LoadETagDB();
44     
45 }
47 void ETagDB::AddETag(wxString Filename, wxString ETag, wxString ETagOriginal){
48     
49         // Lock or wait if it is already locked.
50     
51         ProcessLock->lock();
52     
53         // Process the ETag addition.
54     
55         FilenameETag.insert(std::make_pair(Filename, ETag));
56         FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
57     
58         NeedsWrite = TRUE;
59     
60         // Unlock.
61     
62         ProcessLock->unlock();
63     
64 }
66 void ETagDB::RemoveETag(wxString Filename){
67     
68         // Remove the ETag.
69         
70         // Lock or wait if it is already locked.
71     
72         ProcessLock->lock();
73     
74         // Remove the ETag.
75     
76         Filename.Trim();
77     
78         FilenameETag.erase(Filename);
79         FilenameETagOriginal.erase(Filename);
81         NeedsWrite = TRUE;
82     
83         // Unlock.
84     
85         ProcessLock->unlock();
86     
87 }
89 void ETagDB::UpdateETag(wxString Filename, wxString ETag){
90     
91         // Update the ETag.
92         
93         // Lock or wait if it is already locked.
94     
95         ProcessLock->lock();
96     
97         // Update the ETag.
98     
99         FilenameETag.erase(Filename);
100         FilenameETag.insert(std::make_pair(Filename, ETag));
101     
102         NeedsWrite = TRUE;
103     
104         // Unlock.
105     
106         ProcessLock->unlock();
107     
110 void ETagDB::UpdateETag(wxString Filename, wxString ETag, wxString ETagOriginal){
111     
112         // Update the ETag with the tags given.
113         
114         // Lock or wait if it is already locked.
115     
116         ProcessLock->lock();
117     
118         // Update the ETag.
119     
120         FilenameETag.erase(Filename);
121         FilenameETagOriginal.erase(Filename);
122         FilenameETag.insert(std::make_pair(Filename, ETag));
123         FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
124     
125         std::map<wxString,wxString>::iterator IterWxS;
126         IterWxS = FilenameETagOriginal.find(Filename);
127         NeedsWrite = TRUE;
129         // Unlock.
130     
131         ProcessLock->unlock();
132     
135 bool ETagDB::ETagExists(wxString Filename){
137         // Check if the ETag exists.
138         
139         // Lock or wait if it is already locked.
140     
141         ProcessLock->lock();
142     
143         bool FileResult = FALSE;
144     
145         if (FilenameETag.find(Filename) != FilenameETag.end()){
146         
147                 FileResult = TRUE;
148            
149         } else {
150     
151                 FileResult = FALSE;
152     
153         }
154     
155         // Unlock.
157         ProcessLock->unlock();
158     
159         return FileResult;
163 std::map<wxString,wxString>* ETagDB::GetFilenamePointer(){
164     
165         // Get the Filename Pointer.
166         
167         return &FilenameETag;
168     
171 wxString ETagDB::GetETag(wxString Filename){
173         // Get the ETag.
174         
175         // Lock or wait if it is already locked.
177         wxString ETagOut;
178         std::map<wxString,wxString>::iterator IterwxS;
179     
180         ProcessLock->lock();
181     
182         // Get the ETag.
183         
184         if (FilenameETag.find(Filename) != FilenameETag.end()){
185         
186                 IterwxS = FilenameETag.find(Filename);
187         
188                 ETagOut = IterwxS->second;
189         
190         }
191     
192         // Unlock.
193     
194         ProcessLock->unlock();
195     
196         return ETagOut;
197     
200 wxString ETagDB::GetETagOriginal(wxString Filename){
201     
202         // Get the original ETag.
203         
204         // Lock or wait if it is already locked.
205     
206         wxString ETagOrigOut;
207         std::map<wxString,wxString>::iterator IterwxS;
208     
209         ProcessLock->lock();
210     
211         // Get the ETag.
212     
213         if (FilenameETagOriginal.find(Filename) != FilenameETagOriginal.end()){
214         
215                 IterwxS = FilenameETagOriginal.find(Filename);
216         
217                 ETagOrigOut = IterwxS->second;
218         
219         }
220     
221         // Unlock.
222     
223         ProcessLock->unlock();
224     
225         return ETagOrigOut;
226     
229 bool ETagDB::WriteETagDB(){
230     
231         // Write the ETag database.
232         
233         // Lock or wait if it is already locked.
234     
235         ProcessLock->lock();
236     
237         // Write the ETag database.
238     
239         wxString AccountETagDB;
240         std::map<wxString,wxString>::iterator IterwxS;
241         int LineSeek = 0;
242     
243 #if defined(__HAIKU__)
244     
245         //preffilename = wxT("noo");
246     
247 #elif defined(__WIN32__)
248     
249         AccountETagDB.Clear();
250         AccountETagDB = GetAccountDir(AccountDir, FALSE);
251         AccountETagDB.Append(wxT("\\etag.db"));
252     
253 #else
254     
255         // This entry applies to OSX as well.
256     
257         AccountETagDB.Clear();
258         AccountETagDB = GetAccountDir(AccountDir, FALSE);
259         AccountETagDB.Append(wxT("/etag.db"));
260         
261 #endif
262     
263         wxTextFile ETagDBFile;
264         wxString SettingLine;
265     
266         if (wxFileExists(AccountETagDB) == FALSE){
267         
268                 if (ETagDBFile.Create(AccountETagDB) == FALSE){
269                         
270                         return FALSE;
271                 
272                 }
273         
274         } else {
275         
276                 if (ETagDBFile.Open(AccountETagDB) == FALSE){
277                         
278                         return FALSE;
279         
280                 }
281         
282         }
283     
284         ETagDBFile.Clear();
285     
286         for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
287                 iter != FilenameETag.end(); ++iter){
288         
289                 // Get Original ETag as well.
290         
291                 IterwxS = FilenameETagOriginal.find(iter->first);
292         
293                 if (iter->first.IsEmpty()){
294                         continue;
295                 }
296         
297                 SettingLine = iter->first + wxT("|") + iter->second + wxT("|") + IterwxS->second;
298         
299                 ETagDBFile.InsertLine(SettingLine, LineSeek);
300         
301                 LineSeek++;
302         
303         }
304     
305         ETagDBFile.Write();
306     
307         NeedsWrite = FALSE;
308     
309         // Unlock.
310     
311         ProcessLock->unlock();
312     
313         return TRUE;
314     
317 bool ETagDB::LoadETagDB(){
318     
319         // Load the ETag database.
320         
321         // Lock or wait if it is already locked.
322     
323         ProcessLock->lock();
324     
325         // Load the ETag database.
326     
327         wxString AccountETagDB;
328         wxString AccountDirPath;
329     
330 #if defined(__HAIKU__)
331         
332 #elif defined(__WIN32__)
333     
334         AccountDirPath = GetAccountDir(AccountDir, FALSE);
335     
336         AccountETagDB = AccountDirPath;
337         AccountETagDB.Append(wxT("\\etag.db"));
338     
339 #else
340     
341         AccountDirPath = GetAccountDir(AccountDir, FALSE);
342     
343         AccountETagDB = AccountDirPath;
344         AccountETagDB.Append(wxT("/etag.db"));
345     
346 #endif
347     
348         wxTextFile ETagDBFile;
349     
350         if (wxFileExists(AccountETagDB) == FALSE){
351         
352                 if (ETagDBFile.Create(AccountETagDB) == FALSE){
353                         
354                         return FALSE;
355                         
356                 }
357         
358         } else {
359         
360                 if (ETagDBFile.Open(AccountETagDB, wxConvUTF8) == FALSE){
361                 
362                         return FALSE;
363                 
364                 }
365         
366         }
367     
368         wxString wxSFilename;
369         wxString wxSValue;
370         wxString ETagContactFilename;
371     
372         for (wxStringTokenizer ETagDBLine(ETagDBFile.GetFirstLine(), wxT("|"));
373                 !ETagDBFile.Eof(); ETagDBLine.SetString(ETagDBFile.GetNextLine(), wxT("|"), wxTOKEN_DEFAULT) )
374         {
375         
376                 // Split the file into three.
377         
378                 wxSFilename = ETagDBLine.GetNextToken();
379         
380                 // Get the ETag.
381         
382                 wxSValue = ETagDBLine.GetNextToken();
383                 FilenameETag.insert(std::make_pair(wxSFilename, wxSValue));
384         
385                 // Get the original ETag.
386         
387                 wxSValue = ETagDBLine.GetNextToken();
388                 wxSValue.Trim();
389                 FilenameETagOriginal.insert(std::make_pair(wxSFilename, wxSValue));
390         
391         }
392     
393         // Unlock.
394     
395         ProcessLock->unlock();
396     
397         return TRUE;
398     
401 void ETagDB::DeleteETagDB(){
402     
403         // Delete the ETag database.
404         
405         // Lock or wait if it is already locked.
406     
407         ProcessLock->lock();
408     
409         wxString AccountETagDB;
410     
411         // Delete the ETagDB.
412     
413 #if defined(__HAIKU__)
414     
415         //preffilename = wxT("noo");
416     
417 #elif defined(__WIN32__)
418     
419         AccountETagDB.Clear();
420         AccountETagDB = GetAccountDir(AccountDir, FALSE);
421         AccountETagDB.Append(wxT("\\etag.db"));
422     
423         wxRemoveFile(AccountETagDB);
424     
425 #else
426     
427         AccountETagDB.Clear();
428         AccountETagDB = GetAccountDir(AccountDir, FALSE);
429         AccountETagDB.Append(wxT("/etag.db"));
430     
431         wxRemoveFile(AccountETagDB);
432     
433 #endif
434     
435         // Unlock.
436     
437         ProcessLock->unlock();
438     
441 bool ETagDB::CheckETagExists(wxString Filename){
442     
443         if (FilenameETag.find(Filename) == FilenameETag.end()){
444         
445                 return FALSE;
446         
447         } else {
448         
449                 return TRUE;
450         
451         }       
452     
455 std::map<wxString,ETagData> ETagDB::GetETagData(){
456     
457         // Get the ETag data.
458         
459         // Lock the database.
460     
461         ProcessLock->lock();
462     
463         // Go through each of the ETags and process them accordingly.
464     
465         std::map<wxString,ETagData> DataSet;
466     
467         for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin(); 
468                 iter != FilenameETag.end(); ++iter){
469         
470                 std::map<wxString,wxString>::iterator origiter = FilenameETagOriginal.find(iter->first);
471         
472                 ETagData etd;
473                 
474                 etd.ETagValue = iter->second;
475                 etd.ETagOrigValue = origiter->second;
476         
477                 DataSet.insert(std::make_pair(iter->first, etd));
478         
479         }
480     
481         // Unlock the database.
482     
483         ProcessLock->unlock();
484     
485         return DataSet;
486     
489 bool ETagDB::GetWriteStatus(){
490     
491         // Check if the ETag database needs to be
492         // written to a file.
493         
494         return NeedsWrite;
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