Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Idented code properly for common/etag.cpp
[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         //IterwxS = FilenameETag.find(Filename);
185     
186         if (FilenameETag.find(Filename) != FilenameETag.end()){
187         
188                 IterwxS = FilenameETag.find(Filename);
189         
190                 ETagOut = IterwxS->second;
191         
192         }
193     
194         // Unlock.
195     
196         ProcessLock->unlock();
197     
198         return ETagOut;
199     
202 wxString ETagDB::GetETagOriginal(wxString Filename){
203     
204         // Get the original ETag.
205         
206         // Lock or wait if it is already locked.
207     
208         wxString ETagOrigOut;
209         std::map<wxString,wxString>::iterator IterwxS;
210     
211         ProcessLock->lock();
212     
213         // Get the ETag.
214     
215         //IterwxS = FilenameETag.find(Filename);
216     
217         if (FilenameETagOriginal.find(Filename) != FilenameETagOriginal.end()){
218         
219                 IterwxS = FilenameETagOriginal.find(Filename);
220         
221                 ETagOrigOut = IterwxS->second;
222         
223         }
224     
225         // Unlock.
226     
227         ProcessLock->unlock();
228     
229         return ETagOrigOut;
230     
233 bool ETagDB::WriteETagDB(){
234     
235         // Write the ETag database.
236         
237         // Lock or wait if it is already locked.
238     
239         ProcessLock->lock();
240     
241         // Write the ETag database.
242     
243         wxString AccountETagDB;
244         std::map<wxString,wxString>::iterator IterwxS;
245         int LineSeek = 0;
246     
247 #if defined(__HAIKU__)
248     
249         //preffilename = wxT("noo");
250     
251 #elif defined(__WIN32__)
252     
253         AccountETagDB.Clear();
254         AccountETagDB = GetAccountDir(AccountDir, FALSE);
255         AccountETagDB.Append(wxT("\\etag.db"));
256     
257 #else
258     
259         // This entry applies to OSX as well.
260     
261         AccountETagDB.Clear();
262         AccountETagDB = GetAccountDir(AccountDir, FALSE);
263         AccountETagDB.Append(wxT("/etag.db"));
264         
265 #endif
266     
267         wxTextFile ETagDBFile;
268         wxString SettingLine;
269     
270         if (wxFileExists(AccountETagDB) == FALSE){
271         
272                 if (ETagDBFile.Create(AccountETagDB) == FALSE){
273                         ////ProcessLock.unlock();
274                         return FALSE;
275                 
276                 }
277         
278         } else {
279         
280                 if (ETagDBFile.Open(AccountETagDB) == FALSE){
281                         ////ProcessLock.unlock();
282                         return FALSE;
283         
284                 }
285         
286         }
287     
288         ETagDBFile.Clear();
289     
290         for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
291                 iter != FilenameETag.end(); ++iter){
292         
293                 // Get Original ETag as well.
294         
295                 IterwxS = FilenameETagOriginal.find(iter->first);
296         
297                 if (iter->first.IsEmpty()){
298                         continue;
299                 }
300         
301                 SettingLine = iter->first + wxT("|") + iter->second + wxT("|") + IterwxS->second;
302         
303                 ETagDBFile.InsertLine(SettingLine, LineSeek);
304         
305                 LineSeek++;
306         
307         }
308     
309         ETagDBFile.Write();
310     
311         NeedsWrite = FALSE;
312     
313         // Unlock.
314     
315         ProcessLock->unlock();
316     
317         return TRUE;
318     
321 bool ETagDB::LoadETagDB(){
322     
323         // Load the ETag database.
324         
325         // Lock or wait if it is already locked.
326     
327         ProcessLock->lock();
328     
329         // Load the ETag database.
330     
331         wxString AccountETagDB;
332         wxString AccountDirPath;
333     
334 #if defined(__HAIKU__)
335     
336         //preffilename = wxT("noo");
337     
338 #elif defined(__WIN32__)
339     
340         AccountDirPath = GetAccountDir(AccountDir, FALSE);
341     
342         AccountETagDB = AccountDirPath;
343         AccountETagDB.Append(wxT("\\etag.db"));
344     
345 #else
346     
347         AccountDirPath = GetAccountDir(AccountDir, FALSE);
348     
349         AccountETagDB = AccountDirPath;
350         AccountETagDB.Append(wxT("/etag.db"));
351     
352 #endif
353     
354         wxTextFile ETagDBFile;
355     
356         if (wxFileExists(AccountETagDB) == FALSE){
357         
358                 if (ETagDBFile.Create(AccountETagDB) == FALSE){
359                         ////ProcessLock.unlock();
360                         //ProcessLock->unlock();
361                         
362                         return FALSE;
363                         
364                 }
365         
366         } else {
367         
368                 if (ETagDBFile.Open(AccountETagDB, wxConvUTF8) == FALSE){
369                 
370                         ////ProcessLock.unlock();
371                         //ProcessLock->unlock();
372                         return FALSE;
373                 
374                 }
375         
376         }
377     
378         wxString wxSFilename;
379         wxString wxSValue;
380         wxString ETagContactFilename;
381     
382         for (wxStringTokenizer ETagDBLine(ETagDBFile.GetFirstLine(), wxT("|"));
383                 !ETagDBFile.Eof(); ETagDBLine.SetString(ETagDBFile.GetNextLine(), wxT("|"), wxTOKEN_DEFAULT) )
384         {
385         
386                 // Split the file into three.
387         
388                 wxSFilename = ETagDBLine.GetNextToken();
389         
390                 // Get the ETag.
391         
392                 wxSValue = ETagDBLine.GetNextToken();
393                 FilenameETag.insert(std::make_pair(wxSFilename, wxSValue));
394         
395                 // Get the original ETag.
396         
397                 wxSValue = ETagDBLine.GetNextToken();
398                 wxSValue.Trim();
399                 FilenameETagOriginal.insert(std::make_pair(wxSFilename, wxSValue));
400         
401         }
402     
403         // Unlock.
404     
405         ProcessLock->unlock();
406     
407         return TRUE;
408     
411 void ETagDB::DeleteETagDB(){
412     
413         // Delete the ETag database.
414         
415         // Lock or wait if it is already locked.
416     
417         ProcessLock->lock();
418     
419         wxString AccountETagDB;
420     
421         // Delete the ETagDB.
422     
423 #if defined(__HAIKU__)
424     
425         //preffilename = wxT("noo");
426     
427 #elif defined(__WIN32__)
428     
429         AccountETagDB.Clear();
430         AccountETagDB = GetAccountDir(AccountDir, FALSE);
431         AccountETagDB.Append(wxT("\\etag.db"));
432     
433         wxRemoveFile(AccountETagDB);
434     
435 #else
436     
437         AccountETagDB.Clear();
438         AccountETagDB = GetAccountDir(AccountDir, FALSE);
439         AccountETagDB.Append(wxT("/etag.db"));
440     
441         wxRemoveFile(AccountETagDB);
442     
443 #endif
444     
445         // Unlock.
446     
447         ProcessLock->unlock();
448     
451 bool ETagDB::CheckETagExists(wxString Filename){
452     
453         if (FilenameETag.find(Filename) == FilenameETag.end()){
454         
455                 return FALSE;
456         
457         } else {
458         
459                 return TRUE;
460         
461         }       
462     
465 std::map<wxString,ETagData> ETagDB::GetETagData(){
466     
467         // Get the ETag data.
468         
469         // Lock the database.
470     
471         ProcessLock->lock();
472     
473         // Go through each of the ETags and process them accordingly.
474     
475         std::map<wxString,ETagData> DataSet;
476     
477         for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin(); 
478                 iter != FilenameETag.end(); ++iter){
479         
480                 std::map<wxString,wxString>::iterator origiter = FilenameETagOriginal.find(iter->first);
481         
482                 ETagData etd;
483                 
484                 etd.ETagValue = iter->second;
485                 etd.ETagOrigValue = origiter->second;
486         
487                 DataSet.insert(std::make_pair(iter->first, etd));
488         
489         }
490     
491         // Unlock the database.
492     
493         ProcessLock->unlock();
494     
495         return DataSet;
496     
499 bool ETagDB::GetWriteStatus(){
500     
501         // Check if the ETag database needs to be
502         // written to a file.
503         
504         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