1 // etag.cpp - ETag subroutines.
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
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.
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.
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/>
20 #include "../common/dirs.h"
24 // Write out the database to file.
26 ProcessLock->try_lock();
28 if (NeedsWrite == TRUE){
34 ProcessLock->unlock();
38 void ETagDB::SetupDB(wxString AccountDirInc){
40 AccountDir = AccountDirInc;
45 void ETagDB::AddETag(wxString Filename, wxString ETag, wxString ETagOriginal){
47 // Lock or wait if it is already locked.
51 // Process the ETag addition.
53 FilenameETag.insert(std::make_pair(Filename, ETag));
54 FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
60 ProcessLock->unlock();
64 void ETagDB::RemoveETag(wxString Filename){
66 // Lock or wait if it is already locked.
74 FilenameETag.erase(Filename);
75 FilenameETagOriginal.erase(Filename);
81 ProcessLock->unlock();
85 void ETagDB::UpdateETag(wxString Filename, wxString ETag){
87 // Lock or wait if it is already locked.
93 FilenameETag.erase(Filename);
94 FilenameETag.insert(std::make_pair(Filename, ETag));
100 ProcessLock->unlock();
104 void ETagDB::UpdateETag(wxString Filename, wxString ETag, wxString ETagOriginal){
106 // Lock or wait if it is already locked.
112 FilenameETag.erase(Filename);
113 FilenameETagOriginal.erase(Filename);
114 FilenameETag.insert(std::make_pair(Filename, ETag));
115 FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
117 std::map<wxString,wxString>::iterator IterWxS;
118 IterWxS = FilenameETagOriginal.find(Filename);
123 ProcessLock->unlock();
127 bool ETagDB::ETagExists(wxString Filename){
129 // Lock or wait if it is already locked.
133 bool FileResult = FALSE;
135 if (FilenameETag.find(Filename) != FilenameETag.end()){
147 ProcessLock->unlock();
153 std::map<wxString,wxString>* ETagDB::GetFilenamePointer(){
155 return &FilenameETag;
159 wxString ETagDB::GetETag(wxString Filename){
161 // Lock or wait if it is already locked.
164 std::map<wxString,wxString>::iterator IterwxS;
170 //IterwxS = FilenameETag.find(Filename);
172 if (FilenameETag.find(Filename) != FilenameETag.end()){
174 IterwxS = FilenameETag.find(Filename);
176 ETagOut = IterwxS->second;
182 ProcessLock->unlock();
188 wxString ETagDB::GetETagOriginal(wxString Filename){
190 // Lock or wait if it is already locked.
192 wxString ETagOrigOut;
193 std::map<wxString,wxString>::iterator IterwxS;
199 //IterwxS = FilenameETag.find(Filename);
201 if (FilenameETagOriginal.find(Filename) != FilenameETagOriginal.end()){
203 IterwxS = FilenameETagOriginal.find(Filename);
205 ETagOrigOut = IterwxS->second;
211 ProcessLock->unlock();
217 bool ETagDB::WriteETagDB(){
219 // Lock or wait if it is already locked.
223 // Write the ETag database.
225 wxString AccountETagDB;
226 std::map<wxString,wxString>::iterator IterwxS;
231 #if defined(__HAIKU__)
233 //preffilename = wxT("noo");
235 #elif defined(__WIN32__)
237 AccountETagDB.Clear();
238 AccountETagDB = GetAccountDir(AccountDir, FALSE);
239 AccountETagDB.Append(wxT("\\etag.db"));
243 // This entry applies to OSX as well.
245 AccountETagDB.Clear();
246 AccountETagDB = GetAccountDir(AccountDir, FALSE);
247 AccountETagDB.Append(wxT("/etag.db"));
251 wxTextFile ETagDBFile;
252 wxString SettingLine;
254 if (wxFileExists(AccountETagDB) == FALSE){
256 if (ETagDBFile.Create(AccountETagDB) == FALSE){
257 ////ProcessLock.unlock();
263 if (ETagDBFile.Open(AccountETagDB) == FALSE){
264 ////ProcessLock.unlock();
272 for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
273 iter != FilenameETag.end(); ++iter){
275 // Get Original ETag as well.
277 IterwxS = FilenameETagOriginal.find(iter->first);
279 if (iter->first.IsEmpty()){
283 SettingLine = iter->first + wxT("|") + iter->second + wxT("|") + IterwxS->second;
285 ETagDBFile.InsertLine(SettingLine, LineSeek);
297 ProcessLock->unlock();
303 bool ETagDB::LoadETagDB(){
305 // Lock or wait if it is already locked.
309 // Load the ETag database.
311 wxString AccountETagDB;
312 wxString AccountDirPath;
314 #if defined(__HAIKU__)
316 //preffilename = wxT("noo");
318 #elif defined(__WIN32__)
320 AccountDirPath = GetAccountDir(AccountDir, FALSE);
322 AccountETagDB = AccountDirPath;
323 AccountETagDB.Append(wxT("\\etag.db"));
327 AccountDirPath = GetAccountDir(AccountDir, FALSE);
329 AccountETagDB = AccountDirPath;
330 AccountETagDB.Append(wxT("/etag.db"));
334 wxTextFile ETagDBFile;
336 if (wxFileExists(AccountETagDB) == FALSE){
338 if (ETagDBFile.Create(AccountETagDB) == FALSE){
339 ////ProcessLock.unlock();
340 //ProcessLock->unlock();
346 if (ETagDBFile.Open(AccountETagDB, wxConvUTF8) == FALSE){
347 ////ProcessLock.unlock();
348 //ProcessLock->unlock();
354 wxString wxSFilename;
356 wxString ETagContactFilename;
358 for (wxStringTokenizer ETagDBLine(ETagDBFile.GetFirstLine(), wxT("|"));
359 !ETagDBFile.Eof(); ETagDBLine.SetString(ETagDBFile.GetNextLine(), wxT("|"), wxTOKEN_DEFAULT) )
362 // Split the file into three.
364 wxSFilename = ETagDBLine.GetNextToken();
368 wxSValue = ETagDBLine.GetNextToken();
369 FilenameETag.insert(std::make_pair(wxSFilename, wxSValue));
371 // Get the original ETag.
373 wxSValue = ETagDBLine.GetNextToken();
375 FilenameETagOriginal.insert(std::make_pair(wxSFilename, wxSValue));
381 ProcessLock->unlock();
387 void ETagDB::DeleteETagDB(){
389 // Lock or wait if it is already locked.
393 wxString AccountETagDB;
395 // Delete the ETagDB.
397 #if defined(__HAIKU__)
399 //preffilename = wxT("noo");
401 #elif defined(__WIN32__)
403 AccountETagDB.Clear();
404 AccountETagDB = GetAccountDir(AccountDir, FALSE);
405 AccountETagDB.Append(wxT("\\etag.db"));
407 wxRemoveFile(AccountETagDB);
411 AccountETagDB.Clear();
412 AccountETagDB = GetAccountDir(AccountDir, FALSE);
413 AccountETagDB.Append(wxT("/etag.db"));
415 wxRemoveFile(AccountETagDB);
421 ProcessLock->unlock();
425 bool ETagDB::CheckETagExists(wxString Filename){
427 if (FilenameETag.find(Filename) == FilenameETag.end()){
439 std::map<wxString,ETagData> ETagDB::GetETagData(){
441 // Lock the database.
445 // Go through each of the ETags and process them accordingly.
447 std::map<wxString,ETagData> DataSet;
449 for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
450 iter != FilenameETag.end(); ++iter){
452 std::map<wxString,wxString>::iterator origiter = FilenameETagOriginal.find(iter->first);
456 etd.ETagValue = iter->second;
457 etd.ETagOrigValue = origiter->second;
459 DataSet.insert(std::make_pair(iter->first, etd));
463 // Unlock the database.
465 ProcessLock->unlock();
471 bool ETagDB::GetWriteStatus(){