summaryrefslogtreecommitdiffabout
path: root/pwmanager/pwmanager/pwmdoc.cpp
Side-by-side diff
Diffstat (limited to 'pwmanager/pwmanager/pwmdoc.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--pwmanager/pwmanager/pwmdoc.cpp568
1 files changed, 479 insertions, 89 deletions
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp
index 82fc746..4e8a603 100644
--- a/pwmanager/pwmanager/pwmdoc.cpp
+++ b/pwmanager/pwmanager/pwmdoc.cpp
@@ -35,6 +35,10 @@
#include "kglobal.h"
#endif
+#include <kmessagebox.h>
+#include <libkcal/syncdefines.h>
+
+
#ifdef CONFIG_KWALLETIF
# include "kwalletemu.h"
#endif // CONFIG_KWALLETIF
@@ -1007,7 +1011,7 @@ PwMerror PwMDoc::addEntry(const QString &category, PwMDataItem *d,
d->meta.create = QDateTime::currentDateTime();
d->meta.update = d->meta.create;
}
- dta[cat].d.push_back(*d);
+ dti.dta[cat].d.push_back(*d);
delAllEmptyCat(true);
@@ -1031,9 +1035,9 @@ PwMerror PwMDoc::addCategory(const QString &category, unsigned int *categoryInde
}
PwMCategoryItem item;
item.name = category.latin1();
- dta.push_back(item);
+ dti.dta.push_back(item);
if (categoryIndex)
- *categoryIndex = dta.size() - 1;
+ *categoryIndex = dti.dta.size() - 1;
return e_success;
}
@@ -1053,7 +1057,7 @@ bool PwMDoc::delEntry(unsigned int category, unsigned int index, bool dontFlagDi
{
if (isDeepLocked())
return false;
- if (index > dta[category].d.size() - 1)
+ if (index > dti.dta[category].d.size() - 1)
return false;
getDataChangedLock();
if (!lockAt(category, index, false)) {
@@ -1061,10 +1065,10 @@ bool PwMDoc::delEntry(unsigned int category, unsigned int index, bool dontFlagDi
return false;
}
putDataChangedLock();
- int lvPos = dta[category].d[index].listViewPos;
+ int lvPos = dti.dta[category].d[index].listViewPos;
// delete entry
- dta[category].d.erase(dta[category].d.begin() + index);
+ dti.dta[category].d.erase(dti.dta[category].d.begin() + index);
unsigned int i, entries = numEntries(category);
if (!entries) {
@@ -1076,8 +1080,8 @@ bool PwMDoc::delEntry(unsigned int category, unsigned int index, bool dontFlagDi
}
for (i = 0; i < entries; ++i) {
// decrement all listViewPositions that are greater than the deleted.
- if (dta[category].d[i].listViewPos > lvPos)
- --dta[category].d[i].listViewPos;
+ if (dti.dta[category].d[i].listViewPos > lvPos)
+ --dti.dta[category].d[i].listViewPos;
}
if (!dontFlagDirty)
@@ -1110,7 +1114,7 @@ bool PwMDoc::editEntry(unsigned int oldCategory, const QString &newCategory,
d->meta.create = d->meta.update;
}
}
- if (dta[oldCategory].name != newCategory.latin1()) {
+ if (dti.dta[oldCategory].name != newCategory.latin1()) {
// the user changed the category.
PwMerror ret;
d->rev = 0;
@@ -1120,8 +1124,8 @@ bool PwMDoc::editEntry(unsigned int oldCategory, const QString &newCategory,
if (!delEntry(oldCategory, index, true))
return false;
} else {
- d->rev = dta[oldCategory].d[index].rev + 1; // increment revision counter.
- dta[oldCategory].d[index] = *d;
+ d->rev = dti.dta[oldCategory].d[index].rev + 1; // increment revision counter.
+ dti.dta[oldCategory].d[index] = *d;
}
flagDirty();
return true;
@@ -1143,7 +1147,7 @@ bool PwMDoc::serializeDta(string *d)
{
PWM_ASSERT(d);
Serializer ser;
- if (!ser.serialize(dta))
+ if (!ser.serialize(dti))
return false;
d->assign(ser.getXml());
if (!d->size())
@@ -1159,7 +1163,7 @@ bool PwMDoc::deSerializeDta(const string *d, bool entriesLocked)
Serializer ser(d->c_str());
ser.setDefaultLockStat(entriesLocked);
- if (!ser.deSerialize(&dta))
+ if (!ser.deSerialize(&dti))
return false;
} catch (PwMException) {
return false;
@@ -1167,7 +1171,7 @@ bool PwMDoc::deSerializeDta(const string *d, bool entriesLocked)
#else
Serializer ser(d->c_str());
ser.setDefaultLockStat(entriesLocked);
- if (!ser.deSerialize(&dta))
+ if (!ser.deSerialize(&dti))
return false;
#endif
@@ -1192,7 +1196,7 @@ bool PwMDoc::getEntry(const QString &category, unsigned int index,
bool PwMDoc::getEntry(unsigned int category, unsigned int index,
PwMDataItem *d, bool unlockIfLocked)
{
- if (index > dta[category].d.size() - 1)
+ if (index > dti.dta[category].d.size() - 1)
return false;
bool locked = isLocked(category, index);
@@ -1209,7 +1213,7 @@ bool PwMDoc::getEntry(unsigned int category, unsigned int index,
}
}
- *d = dta[category].d[index];
+ *d = dti.dta[category].d[index];
if (locked)
d->pw = LOCKED_STRING.latin1();
@@ -1227,9 +1231,9 @@ PwMerror PwMDoc::getCommentByLvp(const QString &category, int listViewPos,
unsigned int i, entries = numEntries(cat);
for (i = 0; i < entries; ++i) {
- if (dta[cat].d[i].listViewPos == listViewPos) {
- *foundComment = dta[cat].d[i].comment;
- if (dta[cat].d[i].binary)
+ if (dti.dta[cat].d[i].listViewPos == listViewPos) {
+ *foundComment = dti.dta[cat].d[i].comment;
+ if (dti.dta[cat].d[i].binary)
return e_binEntry;
return e_normalEntry;
}
@@ -1490,7 +1494,7 @@ bool PwMDoc::lockAt(unsigned int category, unsigned int index,
BUG();
return false;
}
- if (lock == dta[category].d[index].lockStat)
+ if (lock == dti.dta[category].d[index].lockStat)
return true;
if (!lock && currentPw != "") {
@@ -1514,8 +1518,8 @@ bool PwMDoc::lockAt(unsigned int category, unsigned int index,
}
}
- dta[category].d[index].lockStat = lock;
- dta[category].d[index].rev++; // increment revision counter.
+ dti.dta[category].d[index].lockStat = lock;
+ dti.dta[category].d[index].rev++; // increment revision counter.
emitDataChanged(this);
if (!lock)
@@ -1571,8 +1575,8 @@ bool PwMDoc::lockAll(bool lock)
}
}
- vector<PwMCategoryItem>::iterator catBegin = dta.begin(),
- catEnd = dta.end(),
+ vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
+ catEnd = dti.dta.end(),
catI = catBegin;
vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
while (catI != catEnd) {
@@ -1645,8 +1649,8 @@ bool PwMDoc::unlockAll_tempoary(bool revert)
timer()->start(DocTimer::id_autoLockTimer);
return false;
}
- vector<PwMCategoryItem>::iterator catBegin = dta.begin(),
- catEnd = dta.end(),
+ vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
+ catEnd = dti.dta.end(),
catI = catBegin;
vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
vector< vector<bool> >::iterator oldCatStatI = oldLockStates->begin();
@@ -1729,8 +1733,8 @@ bool PwMDoc::unlockAll_tempoary(bool revert)
mustUnlock = true;
} else {
// first check if it's needed to unlock some entries
- vector<PwMCategoryItem>::iterator catBegin = dta.begin(),
- catEnd = dta.end(),
+ vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
+ catEnd = dti.dta.end(),
catI = catBegin;
vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
while (catI != catEnd) {
@@ -1775,8 +1779,8 @@ bool PwMDoc::unlockAll_tempoary(bool revert)
timer()->stop(DocTimer::id_autoLockTimer);
oldLockStates = new vector< vector<bool> >;
vector<bool> tmp_vec;
- vector<PwMCategoryItem>::iterator catBegin = dta.begin(),
- catEnd = dta.end(),
+ vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
+ catEnd = dti.dta.end(),
catI = catBegin;
vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
while (catI != catEnd) {
@@ -1860,10 +1864,10 @@ void PwMDoc::_deepUnlock()
void PwMDoc::clearDoc()
{
- dta.clear();
+ dti.clear();
PwMCategoryItem d;
d.name = DEFAULT_CATEGORY.latin1();
- dta.push_back(d);
+ dti.dta.push_back(d);
currentPw = "";
unsetDocStatFlag(DOC_STAT_UNLOCK_WITHOUT_PW);
}
@@ -1898,7 +1902,7 @@ void PwMDoc::setListViewPos(const QString &category, unsigned int index,
void PwMDoc::setListViewPos(unsigned int category, unsigned int index,
int pos)
{
- dta[category].d[index].listViewPos = pos;
+ dti.dta[category].d[index].listViewPos = pos;
/* FIXME workaround: don't flag dirty, because this function sometimes
* get's called when it shouldn't. It's because PwMView assumes
@@ -1918,7 +1922,7 @@ int PwMDoc::getListViewPos(const QString &category, unsigned int index)
return -1;
}
- return dta[cat].d[index].listViewPos;
+ return dti.dta[cat].d[index].listViewPos;
}
void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int searchIn,
@@ -1932,13 +1936,13 @@ void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int sea
unsigned int i, entries = numEntries(category);
for (i = 0; i < entries; ++i) {
if (searchIn & SEARCH_IN_DESC) {
- if (!compareString(find.desc, dta[category].d[i].desc,
+ if (!compareString(find.desc, dti.dta[category].d[i].desc,
caseSensitive, exactWordMatch)) {
continue;
}
}
if (searchIn & SEARCH_IN_NAME) {
- if (!compareString(find.name, dta[category].d[i].name,
+ if (!compareString(find.name, dti.dta[category].d[i].name,
caseSensitive, exactWordMatch)) {
continue;
}
@@ -1947,7 +1951,7 @@ void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int sea
bool wasLocked = isLocked(category, i);
getDataChangedLock();
lockAt(category, i, false);
- if (!compareString(find.pw, dta[category].d[i].pw,
+ if (!compareString(find.pw, dti.dta[category].d[i].pw,
caseSensitive, exactWordMatch)) {
lockAt(category, i, wasLocked);
putDataChangedLock();
@@ -1957,19 +1961,19 @@ void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int sea
putDataChangedLock();
}
if (searchIn & SEARCH_IN_COMMENT) {
- if (!compareString(find.comment, dta[category].d[i].comment,
+ if (!compareString(find.comment, dti.dta[category].d[i].comment,
caseSensitive, exactWordMatch)) {
continue;
}
}
if (searchIn & SEARCH_IN_URL) {
- if (!compareString(find.url, dta[category].d[i].url,
+ if (!compareString(find.url, dti.dta[category].d[i].url,
caseSensitive, exactWordMatch)) {
continue;
}
}
if (searchIn & SEARCH_IN_LAUNCHER) {
- if (!compareString(find.launcher, dta[category].d[i].launcher,
+ if (!compareString(find.launcher, dti.dta[category].d[i].launcher,
caseSensitive, exactWordMatch)) {
continue;
}
@@ -1989,7 +1993,7 @@ void PwMDoc::findEntry(unsigned int category, PwMDataItem find, unsigned int sea
pair<unsigned int, unsigned int> tmp_pair;
for (i = 0; i < items; ++i) {
tmp_pair.first = (*foundPositions)[i];
- tmp_pair.second = dta[category].d[(*foundPositions)[i]].listViewPos;
+ tmp_pair.second = dti.dta[category].d[(*foundPositions)[i]].listViewPos;
tmp_vec.push_back(tmp_pair);
}
sort(tmp_vec.begin(), tmp_vec.end(), dta_lvp_greater());
@@ -2032,12 +2036,12 @@ bool PwMDoc::compareString(const string &s1, const string &s2, bool caseSensitiv
bool PwMDoc::findCategory(const QString &name, unsigned int *index)
{
- vector<PwMCategoryItem>::iterator i = dta.begin(),
- end = dta.end();
+ vector<PwMCategoryItem>::iterator i = dti.dta.begin(),
+ end = dti.dta.end();
while (i != end) {
if ((*i).name == name.latin1()) {
if (index) {
- *index = i - dta.begin();
+ *index = i - dti.dta.begin();
}
return true;
}
@@ -2062,7 +2066,7 @@ bool PwMDoc::renameCategory(unsigned int category, const QString &newName,
if (category > numCategories() - 1)
return false;
- dta[category].name = newName.latin1();
+ dti.dta[category].name = newName.latin1();
if (!dontFlagDirty)
flagDirty();
@@ -2087,7 +2091,7 @@ bool PwMDoc::delCategory(unsigned int category, bool dontFlagDirty)
// We don't delete it, if it is the last existing
// category! Instead we rename it to "Default".
if (numCategories() > 1) {
- dta.erase(dta.begin() + category);
+ dti.dta.erase(dti.dta.begin() + category);
} else {
renameCategory(category, DEFAULT_CATEGORY, dontFlagDirty);
return true;
@@ -2100,8 +2104,8 @@ bool PwMDoc::delCategory(unsigned int category, bool dontFlagDirty)
void PwMDoc::delAllEmptyCat(bool dontFlagDirty)
{
- vector<PwMCategoryItem>::iterator begin = dta.begin(),
- end = dta.end(),
+ vector<PwMCategoryItem>::iterator begin = dti.dta.begin(),
+ end = dti.dta.end(),
i = begin;
while (i != end) {
if (i->d.empty()) {
@@ -2115,8 +2119,8 @@ void PwMDoc::getCategoryList(vector<string> *list)
{
PWM_ASSERT(list);
list->clear();
- vector<PwMCategoryItem>::iterator i = dta.begin(),
- end = dta.end();
+ vector<PwMCategoryItem>::iterator i = dti.dta.begin(),
+ end = dti.dta.end();
while (i != end) {
list->push_back(i->name);
++i;
@@ -2127,8 +2131,8 @@ void PwMDoc::getCategoryList(QStringList *list)
{
PWM_ASSERT(list);
list->clear();
- vector<PwMCategoryItem>::iterator i = dta.begin(),
- end = dta.end();
+ vector<PwMCategoryItem>::iterator i = dti.dta.begin(),
+ end = dti.dta.end();
while (i != end) {
#ifndef PWM_EMBEDDED
list->push_back(i->name.c_str());
@@ -2165,8 +2169,8 @@ void PwMDoc::getEntryList(unsigned int category, vector<string> *list)
{
PWM_ASSERT(list);
list->clear();
- vector<PwMDataItem>::iterator begin = dta[category].d.begin(),
- end = dta[category].d.end(),
+ vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(),
+ end = dti.dta[category].d.end(),
i = begin;
while (i != end) {
list->push_back(i->desc);
@@ -2178,8 +2182,8 @@ void PwMDoc::getEntryList(unsigned int category, QStringList *list)
{
PWM_ASSERT(list);
list->clear();
- vector<PwMDataItem>::iterator begin = dta[category].d.begin(),
- end = dta[category].d.end(),
+ vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(),
+ end = dti.dta[category].d.end(),
i = begin;
while (i != end) {
#ifndef PWM_EMBEDDED
@@ -2207,7 +2211,7 @@ bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex)
rootAlertMsgBox();
return false;
}
- QString command(dta[category].d[entryIndex].launcher.c_str());
+ QString command(dti.dta[category].d[entryIndex].launcher.c_str());
bool wasLocked = isLocked(category, entryIndex);
if (command.find("$p") != -1) {
@@ -2219,17 +2223,17 @@ bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex)
return false;
}
#ifndef PWM_EMBEDDED
- command.replace("$d", dta[category].d[entryIndex].desc.c_str());
- command.replace("$n", dta[category].d[entryIndex].name.c_str());
- command.replace("$p", dta[category].d[entryIndex].pw.c_str());
- command.replace("$u", dta[category].d[entryIndex].url.c_str());
- command.replace("$c", dta[category].d[entryIndex].comment.c_str());
+ command.replace("$d", dti.dta[category].d[entryIndex].desc.c_str());
+ command.replace("$n", dti.dta[category].d[entryIndex].name.c_str());
+ command.replace("$p", dti.dta[category].d[entryIndex].pw.c_str());
+ command.replace("$u", dti.dta[category].d[entryIndex].url.c_str());
+ command.replace("$c", dti.dta[category].d[entryIndex].comment.c_str());
#else
- command.replace(QRegExp("$d"), dta[category].d[entryIndex].desc.c_str());
- command.replace(QRegExp("$n"), dta[category].d[entryIndex].name.c_str());
- command.replace(QRegExp("$p"), dta[category].d[entryIndex].pw.c_str());
- command.replace(QRegExp("$u"), dta[category].d[entryIndex].url.c_str());
- command.replace(QRegExp("$c"), dta[category].d[entryIndex].comment.c_str());
+ command.replace(QRegExp("$d"), dti.dta[category].d[entryIndex].desc.c_str());
+ command.replace(QRegExp("$n"), dti.dta[category].d[entryIndex].name.c_str());
+ command.replace(QRegExp("$p"), dti.dta[category].d[entryIndex].pw.c_str());
+ command.replace(QRegExp("$u"), dti.dta[category].d[entryIndex].url.c_str());
+ command.replace(QRegExp("$c"), dti.dta[category].d[entryIndex].comment.c_str());
#endif
command.append(" &");
@@ -2259,7 +2263,7 @@ bool PwMDoc::goToURL(unsigned int category, unsigned int entryIndex)
rootAlertMsgBox();
return false;
}
- QString url(dta[category].d[entryIndex].url.c_str());
+ QString url(dti.dta[category].d[entryIndex].url.c_str());
if (url.isEmpty())
return false;
@@ -2350,7 +2354,7 @@ PwMerror PwMDoc::exportToText(const QString *file)
numEnt = numEntries(i);
exp = "\n== Category: ";
- exp += dta[i].name;
+ exp += dti.dta[i].name;
exp += " ==\n";
#ifndef PWM_EMBEDDED
if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) {
@@ -2367,27 +2371,27 @@ PwMerror PwMDoc::exportToText(const QString *file)
#endif
for (j = 0; j < numEnt; ++j) {
exp = "\n-- ";
- exp += dta[i].d[j].desc;
+ exp += dti.dta[i].d[j].desc;
exp += " --\n";
exp += i18n("Username: ").latin1();
- exp += dta[i].d[j].name;
+ exp += dti.dta[i].d[j].name;
exp += "\n";
exp += i18n("Password: ").latin1();
- exp += dta[i].d[j].pw;
+ exp += dti.dta[i].d[j].pw;
exp += "\n";
exp += i18n("Comment: ").latin1();
- exp += dta[i].d[j].comment;
+ exp += dti.dta[i].d[j].comment;
exp += "\n";
exp += i18n("URL: ").latin1();
- exp += dta[i].d[j].url;
+ exp += dti.dta[i].d[j].url;
exp += "\n";
exp += i18n("Launcher: ").latin1();
- exp += dta[i].d[j].launcher;
+ exp += dti.dta[i].d[j].launcher;
exp += "\n";
#ifndef PWM_EMBEDDED
@@ -2420,7 +2424,7 @@ PwMerror PwMDoc::importFromText(const QString *file, int format)
// probe for all formats
if (importText_PwM(file) == e_success)
return e_success;
- dta.clear();
+ dti.clear();
emitDataChanged(this);
// add next format here...
return e_fileFormat;
@@ -2614,18 +2618,18 @@ PwMerror PwMDoc::exportToGpasman(const QString *file)
for (i = 0; i < numCat; ++i) {
numEntr = numEntries(i);
for (j = 0; j < numEntr; ++j) {
- descLen = dta[i].d[j].desc.length();
- nameLen = dta[i].d[j].name.length();
- pwLen = dta[i].d[j].pw.length();
- commentLen = dta[i].d[j].comment.length();
+ descLen = dti.dta[i].d[j].desc.length();
+ nameLen = dti.dta[i].d[j].name.length();
+ pwLen = dti.dta[i].d[j].pw.length();
+ commentLen = dti.dta[i].d[j].comment.length();
entry[0] = new char[descLen + 1];
entry[1] = new char[nameLen + 1];
entry[2] = new char[pwLen + 1];
entry[3] = new char[commentLen + 1];
- strcpy(entry[0], descLen == 0 ? " " : dta[i].d[j].desc.c_str());
- strcpy(entry[1], nameLen == 0 ? " " : dta[i].d[j].name.c_str());
- strcpy(entry[2], pwLen == 0 ? " " : dta[i].d[j].pw.c_str());
- strcpy(entry[3], commentLen == 0 ? " " : dta[i].d[j].comment.c_str());
+ strcpy(entry[0], descLen == 0 ? " " : dti.dta[i].d[j].desc.c_str());
+ strcpy(entry[1], nameLen == 0 ? " " : dti.dta[i].d[j].name.c_str());
+ strcpy(entry[2], pwLen == 0 ? " " : dti.dta[i].d[j].pw.c_str());
+ strcpy(entry[3], commentLen == 0 ? " " : dti.dta[i].d[j].comment.c_str());
entry[0][descLen == 0 ? descLen + 1 : descLen] = '\0';
entry[1][nameLen == 0 ? nameLen + 1 : nameLen] = '\0';
entry[2][pwLen == 0 ? pwLen + 1 : pwLen] = '\0';
@@ -2705,8 +2709,8 @@ void PwMDoc::ensureLvp()
vector< vector<PwMDataItem>::iterator >::iterator undefBegin,
undefEnd,
undefI;
- vector<PwMCategoryItem>::iterator catBegin = dta.begin(),
- catEnd = dta.end(),
+ vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(),
+ catEnd = dti.dta.end(),
catI = catBegin;
vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
int lvpTop, tmpLvp;
@@ -2785,9 +2789,9 @@ out_ignore:
#ifdef PWM_EMBEDDED
//US ENH: this is the magic function that syncronizes the this doc with the remote doc
+//US it could have been defined as static, but I did not want to.
PwMerror PwMDoc::syncronize(KSyncManager* manager, PwMDoc* syncLocal , PwMDoc* syncRemote, int mode )
{
- bool syncOK = true;
int addedPasswordsLocal = 0;
int addedPasswordsRemote = 0;
int deletedPasswordsRemote = 0;
@@ -2795,21 +2799,295 @@ PwMerror PwMDoc::syncronize(KSyncManager* manager, PwMDoc* syncLocal , PwMDoc* s
int changedLocal = 0;
int changedRemote = 0;
+ PwMSyncItem* syncItemLocal;
+ PwMSyncItem* syncItemRemote;
+
QString mCurrentSyncName = manager->getCurrentSyncName();
QString mCurrentSyncDevice = manager->getCurrentSyncDevice();
+ bool fullDateRange = false;
+ int take;
+ // local->resetTempSyncStat();
+ QDateTime mLastSync = QDateTime::currentDateTime();
+ QDateTime modifiedSync = mLastSync;
+
+ unsigned int index;
+ //Step 1. Find syncinfo in Local file and create if not existent.
+ bool found = syncLocal->findSyncData(mCurrentSyncDevice, &index);
+ if (found == false)
+ {
+ PwMSyncItem newSyncItemLocal;
+ newSyncItemLocal.syncName = mCurrentSyncDevice;
+ newSyncItemLocal.lastSyncDate = mLastSync;
+ syncLocal->addSyncDataEntry(&newSyncItemLocal, true);
+ found = syncLocal->findSyncData(mCurrentSyncDevice, &index);
+ if (found == false) {
+ qDebug("PwMDoc::syncronize : newly created local sync data could not be found");
+ return e_syncError;
+ }
+ }
+
+ syncItemLocal = syncLocal->getSyncDataEntry(index);
+ qDebug("Last Sync %s ", syncItemLocal->lastSyncDate.toString().latin1());
+
+ //Step 2. Find syncinfo in remote file and create if not existent.
+ found = syncRemote->findSyncData(mCurrentSyncName, &index);
+ if (found == false)
+ {
+ qDebug("FULLDATE 1");
+ fullDateRange = true;
+ PwMSyncItem newSyncItemRemote;
+ newSyncItemRemote.syncName = mCurrentSyncName;
+ newSyncItemRemote.lastSyncDate = mLastSync;
+ syncRemote->addSyncDataEntry(&newSyncItemRemote, true);
+ found = syncRemote->findSyncData(mCurrentSyncName, &index);
+ if (found == false) {
+ qDebug("PwMDoc::syncronize : newly created remote sync data could not be found");
+ return e_syncError;
+ }
+ }
+
+ syncItemRemote = syncRemote->getSyncDataEntry(index);
+ //and remove the found entry here. We will reenter it later again.
+ syncRemote->delSyncDataEntry(index, true);
+ if ( syncItemLocal->lastSyncDate == mLastSync ) {
+ qDebug("FULLDATE 2");
+ fullDateRange = true;
+ }
+
+ if ( ! fullDateRange ) {
+ if ( syncItemLocal->lastSyncDate != syncItemRemote->lastSyncDate ) {
+
+ // qDebug("set fulldate to true %s %s" ,addresseeLSync->dtStart().toString().latin1(), addresseeRSync->dtStart().toString().latin1() );
+ //qDebug("%d %d %d %d ", addresseeLSync->dtStart().time().second(), addresseeLSync->dtStart().time().msec() , addresseeRSync->dtStart().time().second(), addresseeRSync->dtStart().time().msec());
+ fullDateRange = true;
+ qDebug("FULLDATE 3 %s %s", syncItemLocal->lastSyncDate.toString().latin1() , syncItemRemote->lastSyncDate.toString().latin1() );
+ }
+ }
+ // fullDateRange = true; // debug only!
+ if ( fullDateRange )
+ mLastSync = QDateTime::currentDateTime().addDays( -100*365);
+ else
+ mLastSync = syncItemLocal->lastSyncDate;
+
+
+ qDebug("*************************** ");
+ // qDebug("mLastAddressbookSync %s ",mLastAddressbookSync.toString().latin1() );
+ QStringList er = syncRemote->getIDEntryList();
+ PwMDataItem* inRemote ;//= er.first();
+ PwMDataItem* inLocal;
+ unsigned int catLocal, indexLocal;
+ unsigned int catRemote, indexRemote;
+
+ QString uid;
+ manager->showProgressBar(0, i18n("Syncing - close to abort!"), er.count());
+
+ int modulo = (er.count()/10)+1;
+ unsigned int incCounter = 0;
+ while ( incCounter < er.count()) {
+ if (manager->isProgressBarCanceled())
+ return e_syncError;
+ if ( incCounter % modulo == 0 )
+ manager->showProgressBar(incCounter);
+
+ uid = er[ incCounter ];
+ qApp->processEvents();
+
+ inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal );
+ inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote );
+ if ( inLocal != 0 ) { // maybe conflict - same uid in both files
+ if ( (take = takePwMDataItem( inLocal, inRemote, mLastSync, mode, fullDateRange) ) ) {
+ //qDebug("take %d %s ", take, inL.summary().latin1());
+ if ( take == 3 )
+ return e_syncError;
+ if ( take == 1 ) {// take local
+ //US syncRemote->removeAddressee( inRemote );
+ (*inRemote) = (*inLocal);
+ //US syncRemote->insertAddressee( inRemote , false);
+ ++changedRemote;
+ } else { // take == 2 take remote
+ //US syncLocal->removeAddressee( inLocal );
+ (*inLocal) = (*inRemote);
+ //US syncLocal->insertAddressee( inLocal , false );
+ ++changedLocal;
+ }
+ }
+ } else { // no conflict
+ if ( inRemote->meta.update > mLastSync || mode == 5 ) {
+ inRemote->meta.update = modifiedSync;
+ //US syncRemote->insertAddressee( inRemote, false );
+ //US syncLocal->insertAddressee( inRemote, false );
+ syncLocal->addEntry("newcategory", inRemote, true, false);
+
+ ++addedPasswordsLocal;
+ } else {
+ // pending checkExternSyncAddressee(addresseeRSyncSharp, inR);
+ syncRemote->delEntry(catRemote, indexRemote, true);
+ //USsyncRemote->removeAddressee( inRemote );
+ ++deletedPasswordsRemote;
+ }
+ }
+
+ ++incCounter;
+ }
+ er.clear();
+ QStringList el = syncLocal->getIDEntryList();
+ modulo = (el.count()/10)+1;
+
+ manager->showProgressBar(0, i18n("Add / remove addressees"), el.count());
+ incCounter = 0;
+ while ( incCounter < el.count()) {
+ qApp->processEvents();
+ if (manager->isProgressBarCanceled())
+ return e_syncError;
+ if ( incCounter % modulo == 0 )
+ manager->showProgressBar(incCounter);
+ uid = el[ incCounter ];
+
+ inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal );
+ inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote );
+ if ( inRemote == 0 ) {
+ if ( inLocal->meta.update < mLastSync && mode != 4 ) {
+ // pending checkExternSyncAddressee(addresseeLSyncSharp, inL);
+ syncLocal->delEntry(catLocal, indexLocal, true);
+ //USsyncLocal->removeAddressee( inLocal );
+ ++deletedPasswordsLocal;
+ } else {
+ if ( ! PWMPrefs::instance()->mWriteBackExistingOnly ) {
+ ++addedPasswordsRemote;
+ inLocal->meta.update = modifiedSync;
+ //USsyncLocal->insertAddressee( inLocal, false );
+ (*inRemote) = (*inLocal);
+ //USsyncRemote->insertAddressee( inRemote, false );
+ syncRemote->addEntry("newcategory", inRemote, true, false);
+
+ }
+ }
+
+ }
+ ++incCounter;
+ }
+ el.clear();
+ manager->hideProgressBar();
+
+ // Now write the info back into the sync data space of the files
+ mLastSync = QDateTime::currentDateTime().addSecs( 1 );
+ // get rid of micro seconds
+ QTime t = mLastSync.time();
+ mLastSync.setTime( QTime (t.hour (), t.minute (), t.second () ) );
+
+ syncItemLocal->lastSyncDate = mLastSync;
+ syncItemRemote->lastSyncDate = mLastSync;
+ // addresseeRSync.setRole( i18n("!Remote from: ")+mCurrentSyncName ) ;
+ // addresseeLSync.setRole(i18n("!Local from: ") + mCurrentSyncName );
+ syncRemote->addSyncDataEntry( syncItemRemote, false );
+ syncLocal->addSyncDataEntry( syncItemLocal, false );
+ QString mes;
+ mes .sprintf( i18n("Synchronization summary:\n\n %d items added to local\n %d items added to remote\n %d items updated on local\n %d items updated on remote\n %d items deleted on local\n %d items deleted on remote\n"),addedPasswordsLocal, addedPasswordsRemote, changedLocal, changedRemote, deletedPasswordsLocal, deletedPasswordsRemote );
+ if ( PWMPrefs::instance()->mShowSyncSummary ) {
+ KMessageBox::information(0, mes, i18n("PWM/Pi Synchronization") );
+ }
+ qDebug( mes );
return e_success;
}
+int PwMDoc::takePwMDataItem( PwMDataItem* local, PwMDataItem* remote, QDateTime lastSync, int mode , bool full )
+{
+ // 0 equal
+ // 1 take local
+ // 2 take remote
+ // 3 cancel
+ QDateTime localMod = local->meta.update;
+ QDateTime remoteMod = remote->meta.update;
+
+ //US QString mCurrentSyncDevice = syncManager->getCurrentSyncDevice();
+
+ if ( localMod == remoteMod )
+ return 0;
+
+ qDebug(" %d %d conflict on %s %s ", mode, full, local->desc.c_str(), remote->desc.c_str() );
+
+ //qDebug("%s %d %s %d", local->lastModified().toString().latin1() , localMod, remote->lastModified().toString().latin1(), remoteMod);
+ //qDebug("%d %d %d %d ", local->lastModified().time().second(), local->lastModified().time().msec(), remote->lastModified().time().second(), remote->lastModified().time().msec() );
+ //full = true; //debug only
+ if ( full ) {
+ bool equ = true;//US ( (*local) == (*remote) );
+ if ( equ ) {
+ //qDebug("equal ");
+ if ( mode < SYNC_PREF_FORCE_LOCAL )
+ return 0;
+
+ }//else //debug only
+ //qDebug("not equal %s %s ", local->summary().latin1(), remote->summary().latin1());
+ }
+
+ int result;
+ bool localIsNew;
+ //qDebug("%s -- %s mLastCalendarSync %s lastsync %s --- local %s remote %s ",local->summary().latin1(), remote->summary().latin1(),mLastCalendarSync.toString().latin1() ,lastSync.toString().latin1() , local->lastModified().toString().latin1() , remote->lastModified().toString().latin1() );
+
+ if ( full && mode < SYNC_PREF_NEWEST )
+ mode = SYNC_PREF_ASK;
+
+ switch( mode ) {
+ case SYNC_PREF_LOCAL:
+ if ( lastSync > remoteMod )
+ return 1;
+ if ( lastSync > localMod )
+ return 2;
+ return 1;
+ break;
+ case SYNC_PREF_REMOTE:
+ if ( lastSync > remoteMod )
+ return 1;
+ if ( lastSync > localMod )
+ return 2;
+ return 2;
+ break;
+ case SYNC_PREF_NEWEST:
+ if ( localMod > remoteMod )
+ return 1;
+ else
+ return 2;
+ break;
+ case SYNC_PREF_ASK:
+ //qDebug("lsy %s --- lo %s --- re %s ", lastSync.toString().latin1(), localMod.toString().latin1(), remoteMod.toString().latin1() );
+ if ( lastSync > remoteMod )
+ return 1;
+ if ( lastSync > localMod )
+ return 2;
+ localIsNew = localMod >= remoteMod;
+ //qDebug("conflict! ************************************** ");
+ {
+ PwMDataItemChooser acd ( *local,*remote, localIsNew , 0/*this*/ );
+ result = acd.executeD(localIsNew);
+ return result;
+ }
+ break;
+ case SYNC_PREF_FORCE_LOCAL:
+ return 1;
+ break;
+ case SYNC_PREF_FORCE_REMOTE:
+ return 2;
+ break;
+
+ default:
+ // SYNC_PREF_TAKE_BOTH not implemented
+ break;
+ }
+ return 0;
+}
+
+
+
//this are the overwritten callbackmethods from the syncinterface
bool PwMDoc::sync(KSyncManager* manager, QString filename, int mode)
@@ -2861,13 +3139,125 @@ bool PwMDoc::sync_isModified()
//called by the syncmanager to indicate that the work has to be saved.
void PwMDoc::sync_save()
{
- PwMerror ret = saveDoc(conf()->confGlobCompression());
+ saveDoc(conf()->confGlobCompression());
}
-
#endif
+bool PwMDoc::findSyncData(const QString &syncname, unsigned int *index)
+{
+ vector<PwMSyncItem>::iterator i = dti.syncDta.begin(),
+ end = dti.syncDta.end();
+
+ while (i != end) {
+ if ((*i).syncName == syncname.latin1()) {
+ if (index) {
+ *index = i - dti.syncDta.begin();
+ }
+ return true;
+ }
+ ++i;
+ }
+ return false;
+};
+
+/** add new syncdataentry */
+PwMerror PwMDoc::addSyncDataEntry(PwMSyncItem *d, bool dontFlagDirty)
+{
+ PWM_ASSERT(d);
+
+ if (isDeepLocked()) {
+ PwMerror ret;
+ ret = deepLock(false);
+ if (ret != e_success)
+ return e_lock;
+ }
+ unsigned int index;
+
+ const QString tmp = d->syncName.c_str();
+ bool exists = findSyncData(d->syncName.c_str(), &index);
+
+ if (exists == true) {
+ // DOH! We found this entry.
+ return e_entryExists;
+ }
+
+ dti.syncDta.push_back(*d);
+
+ if (!dontFlagDirty)
+ flagDirty();
+ return e_success;
+}
+
+
+/** delete syncdata entry */
+bool PwMDoc::delSyncDataEntry(unsigned int index, bool dontFlagDirty)
+{
+ if (isDeepLocked())
+ return false;
+ if (index > dti.syncDta.size() - 1)
+ return false;
+
+ // delete entry
+ dti.syncDta.erase(dti.syncDta.begin() + index);
+
+ if (!dontFlagDirty)
+ flagDirty();
+ return true;
+}
+
+
+PwMDataItem* PwMDoc::findEntryByID(const QString &uid, unsigned int *category, unsigned int *index)
+{
+ vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(),
+ catend = dti.dta.end();
+
+ vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
+
+ while (catcounter != catend) {
+ entrBegin = catcounter->d.begin();
+ entrEnd = catcounter->d.end();
+ entrI = entrBegin;
+ while (entrI != entrEnd) {
+ if ((*entrI).meta.uniqueid == uid.latin1()) {
+ if (category)
+ *category = catcounter - dti.dta.begin();
+ if (index)
+ *index = entrI - entrBegin;
+
+ return &(*entrI);
+ }
+ ++entrI;
+ }
+ ++catcounter;
+ }
+
+ return 0;
+}
+
+QStringList PwMDoc::getIDEntryList()
+{
+ QStringList results;
+
+ vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(),
+ catend = dti.dta.end();
+
+ vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI;
+
+ while (catcounter != catend) {
+ entrBegin = catcounter->d.begin();
+ entrEnd = catcounter->d.end();
+ entrI = entrBegin;
+ while (entrI != entrEnd) {
+ results.append( (*entrI).meta.uniqueid );
+ ++entrI;
+ }
+ ++catcounter;
+ }
+
+ return results;
+}