Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Move INSTALL, LICENSE, README, THANKS and TODO into root 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         // 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                 wxString ServerETag = "";
299                 if (IterwxS == FilenameETagOriginal.end()) {
301                         ServerETag = "";
303                 } else {
305                         ServerETag = IterwxS->second;
307                 }
309                 SettingLine = iter->first + wxT("|") + iter->second + wxT("|") + ServerETag;
310         
311                 ETagDBFile.InsertLine(SettingLine, LineSeek);
312         
313                 LineSeek++;
314         
315         }
316     
317         ETagDBFile.Write();
318     
319         NeedsWrite = FALSE;
320     
321         // Unlock.
322     
323         ProcessLock->unlock();
324     
325         return TRUE;
326     
329 bool ETagDB::LoadETagDB(){
330     
331         // Load the ETag database.
332         
333         // Lock or wait if it is already locked.
334     
335         ProcessLock->lock();
336     
337         // Load the ETag database.
338     
339         wxString AccountETagDB;
340         wxString AccountDirPath;
341     
342 #if defined(__HAIKU__)
343         
344 #elif defined(__WIN32__)
345     
346         AccountDirPath = GetAccountDir(AccountDir, FALSE);
347     
348         AccountETagDB = AccountDirPath;
349         AccountETagDB.Append(wxT("\\etag.db"));
350     
351 #else
352     
353         AccountDirPath = GetAccountDir(AccountDir, FALSE);
354     
355         AccountETagDB = AccountDirPath;
356         AccountETagDB.Append(wxT("/etag.db"));
357     
358 #endif
359     
360         wxTextFile ETagDBFile;
361     
362         if (wxFileExists(AccountETagDB) == FALSE){
363         
364                 if (ETagDBFile.Create(AccountETagDB) == FALSE){
365                         
366                         return FALSE;
367                         
368                 }
369         
370         } else {
371         
372                 if (ETagDBFile.Open(AccountETagDB, wxConvUTF8) == FALSE){
373                 
374                         return FALSE;
375                 
376                 }
377         
378         }
379     
380         wxString wxSFilename;
381         wxString wxSValue;
382         wxString ETagContactFilename;
383     
384         for (wxStringTokenizer ETagDBLine(ETagDBFile.GetFirstLine(), wxT("|"));
385                 !ETagDBFile.Eof(); ETagDBLine.SetString(ETagDBFile.GetNextLine(), wxT("|"), wxTOKEN_DEFAULT) )
386         {
387         
388                 // Split the file into three.
389         
390                 wxSFilename = ETagDBLine.GetNextToken();
391         
392                 // Get the ETag.
393         
394                 wxSValue = ETagDBLine.GetNextToken();
395                 FilenameETag.insert(std::make_pair(wxSFilename, wxSValue));
396         
397                 // Get the original ETag.
398         
399                 wxSValue = ETagDBLine.GetNextToken();
400                 wxSValue.Trim();
401                 FilenameETagOriginal.insert(std::make_pair(wxSFilename, wxSValue));
402         
403         }
404     
405         // Unlock.
406     
407         ProcessLock->unlock();
408     
409         return TRUE;
410     
413 void ETagDB::DeleteETagDB(){
414     
415         // Delete the ETag database.
416         
417         // Lock or wait if it is already locked.
418     
419         ProcessLock->lock();
420     
421         wxString AccountETagDB;
422     
423         // Delete the ETagDB.
424     
425 #if defined(__HAIKU__)
426     
427         //preffilename = wxT("noo");
428     
429 #elif defined(__WIN32__)
430     
431         AccountETagDB.Clear();
432         AccountETagDB = GetAccountDir(AccountDir, FALSE);
433         AccountETagDB.Append(wxT("\\etag.db"));
434     
435         wxRemoveFile(AccountETagDB);
436     
437 #else
438     
439         AccountETagDB.Clear();
440         AccountETagDB = GetAccountDir(AccountDir, FALSE);
441         AccountETagDB.Append(wxT("/etag.db"));
442     
443         wxRemoveFile(AccountETagDB);
444     
445 #endif
446     
447         // Unlock.
448     
449         ProcessLock->unlock();
450     
453 bool ETagDB::CheckETagExists(wxString Filename){
454     
455         if (FilenameETag.find(Filename) == FilenameETag.end()){
456         
457                 return FALSE;
458         
459         } else {
460         
461                 return TRUE;
462         
463         }       
464     
467 std::map<wxString,ETagData> ETagDB::GetETagData(){
468     
469         // Get the ETag data.
470         
471         // Lock the database.
472     
473         ProcessLock->lock();
474     
475         // Go through each of the ETags and process them accordingly.
476     
477         std::map<wxString,ETagData> DataSet;
478     
479         for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin(); 
480                 iter != FilenameETag.end(); ++iter){
481         
482                 std::map<wxString,wxString>::iterator origiter = FilenameETagOriginal.find(iter->first);
483         
484                 ETagData etd;
485                 
486                 etd.ETagValue = iter->second;
487                 etd.ETagOrigValue = origiter->second;
488         
489                 DataSet.insert(std::make_pair(iter->first, etd));
490         
491         }
492     
493         // Unlock the database.
494     
495         ProcessLock->unlock();
496     
497         return DataSet;
498     
501 bool ETagDB::GetWriteStatus(){
502     
503         // Check if the ETag database needs to be
504         // written to a file.
505         
506         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