-#include "etag.h"
+// etag.cpp - ETag subroutines.
+//
+// (c) 2012-2015 Xestia Software Development.
+//
+// This file is part of Xestia Address Book.
+//
+// Xestia Address Book is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by the
+// Free Software Foundation, version 3 of the license.
+//
+// Xestia Address Book is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
+
+#include "etag.h"
#include "../common/dirs.h"
ETagDB::~ETagDB(){
-
+
// Write out the database to file.
-
- ProcessLock->lock();
-
+
+ ProcessLock->try_lock();
+
if (NeedsWrite == TRUE){
-
+
WriteETagDB();
-
+
}
-
+
ProcessLock->unlock();
-
+
}
void ETagDB::SetupDB(wxString AccountDirInc){
-
+
+ // Setup the ETag Database.
+
AccountDir = AccountDirInc;
LoadETagDB();
- //ProcessLock = ProcessLockInc;
-
+
}
void ETagDB::AddETag(wxString Filename, wxString ETag, wxString ETagOriginal){
-
+
// Lock or wait if it is already locked.
-
+
ProcessLock->lock();
-
+
// Process the ETag addition.
-
+
FilenameETag.insert(std::make_pair(Filename, ETag));
FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
-
+
NeedsWrite = TRUE;
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
}
void ETagDB::RemoveETag(wxString Filename){
-
- // Lock or wait if it is already locked.
+
+ // Remove the ETag.
+ // Lock or wait if it is already locked.
+
ProcessLock->lock();
-
+
// Remove the ETag.
-
+
Filename.Trim();
-
+
FilenameETag.erase(Filename);
FilenameETagOriginal.erase(Filename);
-
+
NeedsWrite = TRUE;
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
}
void ETagDB::UpdateETag(wxString Filename, wxString ETag){
-
- // Lock or wait if it is already locked.
+
+ // Update the ETag.
+ // Lock or wait if it is already locked.
+
ProcessLock->lock();
-
+
// Update the ETag.
-
+
FilenameETag.erase(Filename);
- FilenameETag.insert(std::make_pair(Filename, ETag));
-
+ FilenameETag.insert(std::make_pair(Filename, ETag));
+
NeedsWrite = TRUE;
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
}
void ETagDB::UpdateETag(wxString Filename, wxString ETag, wxString ETagOriginal){
-
- // Lock or wait if it is already locked.
+
+ // Update the ETag with the tags given.
+ // Lock or wait if it is already locked.
+
ProcessLock->lock();
-
+
// Update the ETag.
-
+
FilenameETag.erase(Filename);
FilenameETagOriginal.erase(Filename);
FilenameETag.insert(std::make_pair(Filename, ETag));
FilenameETagOriginal.insert(std::make_pair(Filename, ETagOriginal));
-
+
std::map<wxString,wxString>::iterator IterWxS;
IterWxS = FilenameETagOriginal.find(Filename);
NeedsWrite = TRUE;
-
+
// Unlock.
+
+ ProcessLock->unlock();
+
+}
+
+bool ETagDB::ETagExists(wxString Filename){
+
+ // Check if the ETag exists.
+ // Lock or wait if it is already locked.
+
+ ProcessLock->lock();
+
+ bool FileResult = FALSE;
+
+ if (FilenameETag.find(Filename) != FilenameETag.end()){
+
+ FileResult = TRUE;
+
+ } else {
+
+ FileResult = FALSE;
+
+ }
+
+ // Unlock.
+
ProcessLock->unlock();
+
+ return FileResult;
}
std::map<wxString,wxString>* ETagDB::GetFilenamePointer(){
-
+
+ // Get the Filename Pointer.
+
return &FilenameETag;
-
+
}
wxString ETagDB::GetETag(wxString Filename){
- // Lock or wait if it is already locked.
+ // Get the ETag.
+ // Lock or wait if it is already locked.
+
wxString ETagOut;
std::map<wxString,wxString>::iterator IterwxS;
-
+
ProcessLock->lock();
-
+
// Get the ETag.
-
- //IterwxS = FilenameETag.find(Filename);
-
+
if (FilenameETag.find(Filename) != FilenameETag.end()){
-
+
IterwxS = FilenameETag.find(Filename);
-
+
ETagOut = IterwxS->second;
-
+
}
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
return ETagOut;
-
+
}
wxString ETagDB::GetETagOriginal(wxString Filename){
-
- // Lock or wait if it is already locked.
+
+ // Get the original ETag.
+ // Lock or wait if it is already locked.
+
wxString ETagOrigOut;
std::map<wxString,wxString>::iterator IterwxS;
-
+
ProcessLock->lock();
-
+
// Get the ETag.
-
- //IterwxS = FilenameETag.find(Filename);
-
+
if (FilenameETagOriginal.find(Filename) != FilenameETagOriginal.end()){
-
+
IterwxS = FilenameETagOriginal.find(Filename);
-
+
ETagOrigOut = IterwxS->second;
-
+
}
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
return ETagOrigOut;
-
+
}
-
-bool ETagDB::WriteETagDB(){
- // Lock or wait if it is already locked.
+bool ETagDB::WriteETagDB(){
+
+ // Write the ETag database.
+ // Lock or wait if it is already locked.
+
ProcessLock->lock();
-
+
// Write the ETag database.
-
+
wxString AccountETagDB;
std::map<wxString,wxString>::iterator IterwxS;
int LineSeek = 0;
-
-
-
+
#if defined(__HAIKU__)
- //preffilename = wxT("noo");
-
+ //preffilename = wxT("noo");
+
#elif defined(__WIN32__)
-
+
AccountETagDB.Clear();
AccountETagDB = GetAccountDir(AccountDir, FALSE);
AccountETagDB.Append(wxT("\\etag.db"));
-
+
#else
-
+
+ // This entry applies to OSX as well.
+
AccountETagDB.Clear();
- AccountETagDB = GetAccountDir(AccountDir, FALSE);
+ AccountETagDB = GetAccountDir(AccountDir, FALSE);
AccountETagDB.Append(wxT("/etag.db"));
-
+
#endif
-
+
wxTextFile ETagDBFile;
wxString SettingLine;
-
+
if (wxFileExists(AccountETagDB) == FALSE){
-
+
if (ETagDBFile.Create(AccountETagDB) == FALSE){
- ////ProcessLock.unlock();
+
return FALSE;
+
}
-
+
} else {
-
+
if (ETagDBFile.Open(AccountETagDB) == FALSE){
- ////ProcessLock.unlock();
+
return FALSE;
+
}
-
+
}
-
+
ETagDBFile.Clear();
-
- for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
- iter != FilenameETag.end(); ++iter){
-
+
+ for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
+ iter != FilenameETag.end(); ++iter){
+
// Get Original ETag as well.
-
+
IterwxS = FilenameETagOriginal.find(iter->first);
-
+
if (iter->first.IsEmpty()){
continue;
}
-
+
SettingLine = iter->first + wxT("|") + iter->second + wxT("|") + IterwxS->second;
-
+
ETagDBFile.InsertLine(SettingLine, LineSeek);
-
+
LineSeek++;
-
+
}
-
+
ETagDBFile.Write();
-
+
NeedsWrite = FALSE;
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
return TRUE;
-
+
}
bool ETagDB::LoadETagDB(){
-
- // Lock or wait if it is already locked.
+
+ // Load the ETag database.
+ // Lock or wait if it is already locked.
+
ProcessLock->lock();
-
+
// Load the ETag database.
-
+
wxString AccountETagDB;
wxString AccountDirPath;
-
-#if defined(__HAIKU__)
- //preffilename = wxT("noo");
-
+#if defined(__HAIKU__)
+
#elif defined(__WIN32__)
-
+
AccountDirPath = GetAccountDir(AccountDir, FALSE);
-
+
AccountETagDB = AccountDirPath;
AccountETagDB.Append(wxT("\\etag.db"));
-
+
#else
-
- AccountDirPath = GetAccountDir(AccountDir, bool FALSE);
-
+
+ AccountDirPath = GetAccountDir(AccountDir, FALSE);
+
AccountETagDB = AccountDirPath;
AccountETagDB.Append(wxT("/etag.db"));
-
+
#endif
-
+
wxTextFile ETagDBFile;
-
+
if (wxFileExists(AccountETagDB) == FALSE){
-
+
if (ETagDBFile.Create(AccountETagDB) == FALSE){
- ////ProcessLock.unlock();
- //ProcessLock->unlock();
- return FALSE;
+
+ return FALSE;
+
}
-
+
} else {
-
+
if (ETagDBFile.Open(AccountETagDB, wxConvUTF8) == FALSE){
- ////ProcessLock.unlock();
- //ProcessLock->unlock();
+
return FALSE;
- }
+ }
+
}
-
+
wxString wxSFilename;
wxString wxSValue;
wxString ETagContactFilename;
-
+
for (wxStringTokenizer ETagDBLine(ETagDBFile.GetFirstLine(), wxT("|"));
!ETagDBFile.Eof(); ETagDBLine.SetString(ETagDBFile.GetNextLine(), wxT("|"), wxTOKEN_DEFAULT) )
{
-
+
// Split the file into three.
-
+
wxSFilename = ETagDBLine.GetNextToken();
-
+
// Get the ETag.
-
+
wxSValue = ETagDBLine.GetNextToken();
FilenameETag.insert(std::make_pair(wxSFilename, wxSValue));
-
+
// Get the original ETag.
-
+
wxSValue = ETagDBLine.GetNextToken();
wxSValue.Trim();
FilenameETagOriginal.insert(std::make_pair(wxSFilename, wxSValue));
-
+
}
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
return TRUE;
-
+
}
-void ETagDB::DeleteETagDB(){
-
- // Lock or wait if it is already locked.
+void ETagDB::DeleteETagDB(){
+
+ // Delete the ETag database.
+ // Lock or wait if it is already locked.
+
ProcessLock->lock();
-
+
wxString AccountETagDB;
-
+
// Delete the ETagDB.
-
+
#if defined(__HAIKU__)
- //preffilename = wxT("noo");
-
+ //preffilename = wxT("noo");
+
#elif defined(__WIN32__)
-
+
AccountETagDB.Clear();
AccountETagDB = GetAccountDir(AccountDir, FALSE);
AccountETagDB.Append(wxT("\\etag.db"));
-
+
wxRemoveFile(AccountETagDB);
-
+
#else
-
+
AccountETagDB.Clear();
AccountETagDB = GetAccountDir(AccountDir, FALSE);
AccountETagDB.Append(wxT("/etag.db"));
-
+
wxRemoveFile(AccountETagDB);
-
+
#endif
-
+
// Unlock.
-
+
ProcessLock->unlock();
-
+
}
bool ETagDB::CheckETagExists(wxString Filename){
-
+
if (FilenameETag.find(Filename) == FilenameETag.end()){
-
+
return FALSE;
-
+
} else {
-
+
return TRUE;
-
+
}
-
+
}
std::map<wxString,ETagData> ETagDB::GetETagData(){
-
- // Lock the database.
+
+ // Get the ETag data.
+ // Lock the database.
+
ProcessLock->lock();
-
+
// Go through each of the ETags and process them accordingly.
-
+
std::map<wxString,ETagData> DataSet;
-
+
for (std::map<wxString,wxString>::iterator iter = FilenameETag.begin();
- iter != FilenameETag.end(); ++iter){
-
+ iter != FilenameETag.end(); ++iter){
+
std::map<wxString,wxString>::iterator origiter = FilenameETagOriginal.find(iter->first);
-
+
ETagData etd;
etd.ETagValue = iter->second;
etd.ETagOrigValue = origiter->second;
-
+
DataSet.insert(std::make_pair(iter->first, etd));
-
+
}
-
+
// Unlock the database.
-
+
ProcessLock->unlock();
-
+
return DataSet;
-
+
}
bool ETagDB::GetWriteStatus(){
-
+
+ // Check if the ETag database needs to be
+ // written to a file.
+
return NeedsWrite;
}
\ No newline at end of file