-rw-r--r-- | pwmanager/pwmanager/pwmdoc.cpp | 48 |
1 files changed, 40 insertions, 8 deletions
diff --git a/pwmanager/pwmanager/pwmdoc.cpp b/pwmanager/pwmanager/pwmdoc.cpp index 4e8a603..6c1a9c0 100644 --- a/pwmanager/pwmanager/pwmdoc.cpp +++ b/pwmanager/pwmanager/pwmdoc.cpp | |||
@@ -2128,1141 +2128,1173 @@ void PwMDoc::getCategoryList(vector<string> *list) | |||
2128 | } | 2128 | } |
2129 | 2129 | ||
2130 | void PwMDoc::getCategoryList(QStringList *list) | 2130 | void PwMDoc::getCategoryList(QStringList *list) |
2131 | { | 2131 | { |
2132 | PWM_ASSERT(list); | 2132 | PWM_ASSERT(list); |
2133 | list->clear(); | 2133 | list->clear(); |
2134 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), | 2134 | vector<PwMCategoryItem>::iterator i = dti.dta.begin(), |
2135 | end = dti.dta.end(); | 2135 | end = dti.dta.end(); |
2136 | while (i != end) { | 2136 | while (i != end) { |
2137 | #ifndef PWM_EMBEDDED | 2137 | #ifndef PWM_EMBEDDED |
2138 | list->push_back(i->name.c_str()); | 2138 | list->push_back(i->name.c_str()); |
2139 | #else | 2139 | #else |
2140 | list->append(i->name.c_str()); | 2140 | list->append(i->name.c_str()); |
2141 | #endif | 2141 | #endif |
2142 | ++i; | 2142 | ++i; |
2143 | } | 2143 | } |
2144 | } | 2144 | } |
2145 | 2145 | ||
2146 | void PwMDoc::getEntryList(const QString &category, QStringList *list) | 2146 | void PwMDoc::getEntryList(const QString &category, QStringList *list) |
2147 | { | 2147 | { |
2148 | PWM_ASSERT(list); | 2148 | PWM_ASSERT(list); |
2149 | unsigned int cat = 0; | 2149 | unsigned int cat = 0; |
2150 | if (!findCategory(category, &cat)) { | 2150 | if (!findCategory(category, &cat)) { |
2151 | list->clear(); | 2151 | list->clear(); |
2152 | return; | 2152 | return; |
2153 | } | 2153 | } |
2154 | getEntryList(cat, list); | 2154 | getEntryList(cat, list); |
2155 | } | 2155 | } |
2156 | 2156 | ||
2157 | void PwMDoc::getEntryList(const QString &category, vector<string> *list) | 2157 | void PwMDoc::getEntryList(const QString &category, vector<string> *list) |
2158 | { | 2158 | { |
2159 | PWM_ASSERT(list); | 2159 | PWM_ASSERT(list); |
2160 | unsigned int cat = 0; | 2160 | unsigned int cat = 0; |
2161 | if (!findCategory(category, &cat)) { | 2161 | if (!findCategory(category, &cat)) { |
2162 | list->clear(); | 2162 | list->clear(); |
2163 | return; | 2163 | return; |
2164 | } | 2164 | } |
2165 | getEntryList(cat, list); | 2165 | getEntryList(cat, list); |
2166 | } | 2166 | } |
2167 | 2167 | ||
2168 | void PwMDoc::getEntryList(unsigned int category, vector<string> *list) | 2168 | void PwMDoc::getEntryList(unsigned int category, vector<string> *list) |
2169 | { | 2169 | { |
2170 | PWM_ASSERT(list); | 2170 | PWM_ASSERT(list); |
2171 | list->clear(); | 2171 | list->clear(); |
2172 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), | 2172 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), |
2173 | end = dti.dta[category].d.end(), | 2173 | end = dti.dta[category].d.end(), |
2174 | i = begin; | 2174 | i = begin; |
2175 | while (i != end) { | 2175 | while (i != end) { |
2176 | list->push_back(i->desc); | 2176 | list->push_back(i->desc); |
2177 | ++i; | 2177 | ++i; |
2178 | } | 2178 | } |
2179 | } | 2179 | } |
2180 | 2180 | ||
2181 | void PwMDoc::getEntryList(unsigned int category, QStringList *list) | 2181 | void PwMDoc::getEntryList(unsigned int category, QStringList *list) |
2182 | { | 2182 | { |
2183 | PWM_ASSERT(list); | 2183 | PWM_ASSERT(list); |
2184 | list->clear(); | 2184 | list->clear(); |
2185 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), | 2185 | vector<PwMDataItem>::iterator begin = dti.dta[category].d.begin(), |
2186 | end = dti.dta[category].d.end(), | 2186 | end = dti.dta[category].d.end(), |
2187 | i = begin; | 2187 | i = begin; |
2188 | while (i != end) { | 2188 | while (i != end) { |
2189 | #ifndef PWM_EMBEDDED | 2189 | #ifndef PWM_EMBEDDED |
2190 | list->push_back(i->desc.c_str()); | 2190 | list->push_back(i->desc.c_str()); |
2191 | #else | 2191 | #else |
2192 | list->append(i->desc.c_str()); | 2192 | list->append(i->desc.c_str()); |
2193 | #endif | 2193 | #endif |
2194 | ++i; | 2194 | ++i; |
2195 | } | 2195 | } |
2196 | } | 2196 | } |
2197 | 2197 | ||
2198 | bool PwMDoc::execLauncher(const QString &category, unsigned int entryIndex) | 2198 | bool PwMDoc::execLauncher(const QString &category, unsigned int entryIndex) |
2199 | { | 2199 | { |
2200 | unsigned int cat = 0; | 2200 | unsigned int cat = 0; |
2201 | 2201 | ||
2202 | if (!findCategory(category, &cat)) | 2202 | if (!findCategory(category, &cat)) |
2203 | return false; | 2203 | return false; |
2204 | 2204 | ||
2205 | return execLauncher(cat, entryIndex); | 2205 | return execLauncher(cat, entryIndex); |
2206 | } | 2206 | } |
2207 | 2207 | ||
2208 | bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex) | 2208 | bool PwMDoc::execLauncher(unsigned int category, unsigned int entryIndex) |
2209 | { | 2209 | { |
2210 | if (geteuid() == 0) { | 2210 | if (geteuid() == 0) { |
2211 | rootAlertMsgBox(); | 2211 | rootAlertMsgBox(); |
2212 | return false; | 2212 | return false; |
2213 | } | 2213 | } |
2214 | QString command(dti.dta[category].d[entryIndex].launcher.c_str()); | 2214 | QString command(dti.dta[category].d[entryIndex].launcher.c_str()); |
2215 | bool wasLocked = isLocked(category, entryIndex); | 2215 | bool wasLocked = isLocked(category, entryIndex); |
2216 | 2216 | ||
2217 | if (command.find("$p") != -1) { | 2217 | if (command.find("$p") != -1) { |
2218 | /* the user requested the password to be included | 2218 | /* the user requested the password to be included |
2219 | * into the command. We have to ask for the password, | 2219 | * into the command. We have to ask for the password, |
2220 | * if it's locked. We do that by unlocking the entry | 2220 | * if it's locked. We do that by unlocking the entry |
2221 | */ | 2221 | */ |
2222 | if (!lockAt(category, entryIndex, false)) | 2222 | if (!lockAt(category, entryIndex, false)) |
2223 | return false; | 2223 | return false; |
2224 | } | 2224 | } |
2225 | #ifndef PWM_EMBEDDED | 2225 | #ifndef PWM_EMBEDDED |
2226 | command.replace("$d", dti.dta[category].d[entryIndex].desc.c_str()); | 2226 | command.replace("$d", dti.dta[category].d[entryIndex].desc.c_str()); |
2227 | command.replace("$n", dti.dta[category].d[entryIndex].name.c_str()); | 2227 | command.replace("$n", dti.dta[category].d[entryIndex].name.c_str()); |
2228 | command.replace("$p", dti.dta[category].d[entryIndex].pw.c_str()); | 2228 | command.replace("$p", dti.dta[category].d[entryIndex].pw.c_str()); |
2229 | command.replace("$u", dti.dta[category].d[entryIndex].url.c_str()); | 2229 | command.replace("$u", dti.dta[category].d[entryIndex].url.c_str()); |
2230 | command.replace("$c", dti.dta[category].d[entryIndex].comment.c_str()); | 2230 | command.replace("$c", dti.dta[category].d[entryIndex].comment.c_str()); |
2231 | #else | 2231 | #else |
2232 | command.replace(QRegExp("$d"), dti.dta[category].d[entryIndex].desc.c_str()); | 2232 | command.replace(QRegExp("$d"), dti.dta[category].d[entryIndex].desc.c_str()); |
2233 | command.replace(QRegExp("$n"), dti.dta[category].d[entryIndex].name.c_str()); | 2233 | command.replace(QRegExp("$n"), dti.dta[category].d[entryIndex].name.c_str()); |
2234 | command.replace(QRegExp("$p"), dti.dta[category].d[entryIndex].pw.c_str()); | 2234 | command.replace(QRegExp("$p"), dti.dta[category].d[entryIndex].pw.c_str()); |
2235 | command.replace(QRegExp("$u"), dti.dta[category].d[entryIndex].url.c_str()); | 2235 | command.replace(QRegExp("$u"), dti.dta[category].d[entryIndex].url.c_str()); |
2236 | command.replace(QRegExp("$c"), dti.dta[category].d[entryIndex].comment.c_str()); | 2236 | command.replace(QRegExp("$c"), dti.dta[category].d[entryIndex].comment.c_str()); |
2237 | #endif | 2237 | #endif |
2238 | command.append(" &"); | 2238 | command.append(" &"); |
2239 | 2239 | ||
2240 | QString customXterm(conf()->confGlobXtermCommand()); | 2240 | QString customXterm(conf()->confGlobXtermCommand()); |
2241 | if (!customXterm.isEmpty()) | 2241 | if (!customXterm.isEmpty()) |
2242 | command = customXterm + " " + command; | 2242 | command = customXterm + " " + command; |
2243 | 2243 | ||
2244 | system(command.latin1()); | 2244 | system(command.latin1()); |
2245 | 2245 | ||
2246 | lockAt(category, entryIndex, wasLocked); | 2246 | lockAt(category, entryIndex, wasLocked); |
2247 | return true; | 2247 | return true; |
2248 | } | 2248 | } |
2249 | 2249 | ||
2250 | bool PwMDoc::goToURL(const QString &category, unsigned int entryIndex) | 2250 | bool PwMDoc::goToURL(const QString &category, unsigned int entryIndex) |
2251 | { | 2251 | { |
2252 | unsigned int cat = 0; | 2252 | unsigned int cat = 0; |
2253 | 2253 | ||
2254 | if (!findCategory(category, &cat)) | 2254 | if (!findCategory(category, &cat)) |
2255 | return false; | 2255 | return false; |
2256 | 2256 | ||
2257 | return goToURL(cat, entryIndex); | 2257 | return goToURL(cat, entryIndex); |
2258 | } | 2258 | } |
2259 | 2259 | ||
2260 | bool PwMDoc::goToURL(unsigned int category, unsigned int entryIndex) | 2260 | bool PwMDoc::goToURL(unsigned int category, unsigned int entryIndex) |
2261 | { | 2261 | { |
2262 | if (geteuid() == 0) { | 2262 | if (geteuid() == 0) { |
2263 | rootAlertMsgBox(); | 2263 | rootAlertMsgBox(); |
2264 | return false; | 2264 | return false; |
2265 | } | 2265 | } |
2266 | QString url(dti.dta[category].d[entryIndex].url.c_str()); | 2266 | QString url(dti.dta[category].d[entryIndex].url.c_str()); |
2267 | if (url.isEmpty()) | 2267 | if (url.isEmpty()) |
2268 | return false; | 2268 | return false; |
2269 | 2269 | ||
2270 | QString customBrowser(conf()->confGlobBrowserCommand()); | 2270 | QString customBrowser(conf()->confGlobBrowserCommand()); |
2271 | if (!customBrowser.isEmpty()) { | 2271 | if (!customBrowser.isEmpty()) { |
2272 | browserProc.clearArguments(); | 2272 | browserProc.clearArguments(); |
2273 | browserProc << customBrowser << url; | 2273 | browserProc << customBrowser << url; |
2274 | if (browserProc.start(KProcess::DontCare)) | 2274 | if (browserProc.start(KProcess::DontCare)) |
2275 | return true; | 2275 | return true; |
2276 | } | 2276 | } |
2277 | 2277 | ||
2278 | browserProc.clearArguments(); | 2278 | browserProc.clearArguments(); |
2279 | browserProc << "konqueror" << url; | 2279 | browserProc << "konqueror" << url; |
2280 | if (browserProc.start(KProcess::DontCare)) | 2280 | if (browserProc.start(KProcess::DontCare)) |
2281 | return true; | 2281 | return true; |
2282 | 2282 | ||
2283 | browserProc.clearArguments(); | 2283 | browserProc.clearArguments(); |
2284 | browserProc << "mozilla" << url; | 2284 | browserProc << "mozilla" << url; |
2285 | if (browserProc.start(KProcess::DontCare)) | 2285 | if (browserProc.start(KProcess::DontCare)) |
2286 | return true; | 2286 | return true; |
2287 | 2287 | ||
2288 | browserProc.clearArguments(); | 2288 | browserProc.clearArguments(); |
2289 | browserProc << "opera" << url; | 2289 | browserProc << "opera" << url; |
2290 | if (browserProc.start(KProcess::DontCare)) | 2290 | if (browserProc.start(KProcess::DontCare)) |
2291 | return true; | 2291 | return true; |
2292 | return false; | 2292 | return false; |
2293 | } | 2293 | } |
2294 | 2294 | ||
2295 | PwMerror PwMDoc::exportToText(const QString *file) | 2295 | PwMerror PwMDoc::exportToText(const QString *file) |
2296 | { | 2296 | { |
2297 | PWM_ASSERT(file); | 2297 | PWM_ASSERT(file); |
2298 | if (QFile::exists(*file)) { | 2298 | if (QFile::exists(*file)) { |
2299 | if (!QFile::remove(*file)) | 2299 | if (!QFile::remove(*file)) |
2300 | return e_accessFile; | 2300 | return e_accessFile; |
2301 | } | 2301 | } |
2302 | QFile f(*file); | 2302 | QFile f(*file); |
2303 | if (!f.open(IO_ReadWrite)) | 2303 | if (!f.open(IO_ReadWrite)) |
2304 | return e_openFile; | 2304 | return e_openFile; |
2305 | 2305 | ||
2306 | if (!unlockAll_tempoary()) { | 2306 | if (!unlockAll_tempoary()) { |
2307 | f.close(); | 2307 | f.close(); |
2308 | return e_lock; | 2308 | return e_lock; |
2309 | } | 2309 | } |
2310 | 2310 | ||
2311 | // write header | 2311 | // write header |
2312 | string header = i18n("Password table generated by\nPwM v").latin1(); | 2312 | string header = i18n("Password table generated by\nPwM v").latin1(); |
2313 | header += PACKAGE_VER; | 2313 | header += PACKAGE_VER; |
2314 | header += i18n("\non ").latin1(); | 2314 | header += i18n("\non ").latin1(); |
2315 | QDate currDate = QDate::currentDate(); | 2315 | QDate currDate = QDate::currentDate(); |
2316 | QTime currTime = QTime::currentTime(); | 2316 | QTime currTime = QTime::currentTime(); |
2317 | 2317 | ||
2318 | #ifndef PWM_EMBEDDED | 2318 | #ifndef PWM_EMBEDDED |
2319 | header += currDate.toString("ddd MMMM d ").latin1(); | 2319 | header += currDate.toString("ddd MMMM d ").latin1(); |
2320 | header += currTime.toString("hh:mm:ss ").latin1(); | 2320 | header += currTime.toString("hh:mm:ss ").latin1(); |
2321 | #else | 2321 | #else |
2322 | QString dfs = KGlobal::locale()->dateFormatShort(); | 2322 | QString dfs = KGlobal::locale()->dateFormatShort(); |
2323 | bool ampm = KGlobal::locale()->use12Clock(); | 2323 | bool ampm = KGlobal::locale()->use12Clock(); |
2324 | KGlobal::locale()->setDateFormatShort("%A %B %d"); | 2324 | KGlobal::locale()->setDateFormatShort("%A %B %d"); |
2325 | KGlobal::locale()->setHore24Format(true); | 2325 | KGlobal::locale()->setHore24Format(true); |
2326 | 2326 | ||
2327 | header += KGlobal::locale()->formatDate(currDate, true, KLocale::Userdefined); | 2327 | header += KGlobal::locale()->formatDate(currDate, true, KLocale::Userdefined); |
2328 | header += KGlobal::locale()->formatTime(currTime, true); | 2328 | header += KGlobal::locale()->formatTime(currTime, true); |
2329 | KGlobal::locale()->setDateFormatShort(dfs); | 2329 | KGlobal::locale()->setDateFormatShort(dfs); |
2330 | KGlobal::locale()->setHore24Format(!ampm); | 2330 | KGlobal::locale()->setHore24Format(!ampm); |
2331 | 2331 | ||
2332 | #endif | 2332 | #endif |
2333 | header += tostr(currDate.year()); | 2333 | header += tostr(currDate.year()); |
2334 | header += "\n==============================\n\n"; | 2334 | header += "\n==============================\n\n"; |
2335 | 2335 | ||
2336 | 2336 | ||
2337 | #ifndef PWM_EMBEDDED | 2337 | #ifndef PWM_EMBEDDED |
2338 | if (f.writeBlock(header.c_str(), header.length()) != (Q_LONG)header.length()) { | 2338 | if (f.writeBlock(header.c_str(), header.length()) != (Q_LONG)header.length()) { |
2339 | unlockAll_tempoary(true); | 2339 | unlockAll_tempoary(true); |
2340 | f.close(); | 2340 | f.close(); |
2341 | return e_writeFile; | 2341 | return e_writeFile; |
2342 | } | 2342 | } |
2343 | #else | 2343 | #else |
2344 | if (f.writeBlock(header.c_str(), header.length()) != (long)header.length()) { | 2344 | if (f.writeBlock(header.c_str(), header.length()) != (long)header.length()) { |
2345 | unlockAll_tempoary(true); | 2345 | unlockAll_tempoary(true); |
2346 | f.close(); | 2346 | f.close(); |
2347 | return e_writeFile; | 2347 | return e_writeFile; |
2348 | } | 2348 | } |
2349 | #endif | 2349 | #endif |
2350 | unsigned int i, numCat = numCategories(); | 2350 | unsigned int i, numCat = numCategories(); |
2351 | unsigned int j, numEnt; | 2351 | unsigned int j, numEnt; |
2352 | string exp; | 2352 | string exp; |
2353 | for (i = 0; i < numCat; ++i) { | 2353 | for (i = 0; i < numCat; ++i) { |
2354 | numEnt = numEntries(i); | 2354 | numEnt = numEntries(i); |
2355 | 2355 | ||
2356 | exp = "\n== Category: "; | 2356 | exp = "\n== Category: "; |
2357 | exp += dti.dta[i].name; | 2357 | exp += dti.dta[i].name; |
2358 | exp += " ==\n"; | 2358 | exp += " ==\n"; |
2359 | #ifndef PWM_EMBEDDED | 2359 | #ifndef PWM_EMBEDDED |
2360 | if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) { | 2360 | if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) { |
2361 | unlockAll_tempoary(true); | 2361 | unlockAll_tempoary(true); |
2362 | f.close(); | 2362 | f.close(); |
2363 | return e_writeFile; | 2363 | return e_writeFile; |
2364 | } | 2364 | } |
2365 | #else | 2365 | #else |
2366 | if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) { | 2366 | if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) { |
2367 | unlockAll_tempoary(true); | 2367 | unlockAll_tempoary(true); |
2368 | f.close(); | 2368 | f.close(); |
2369 | return e_writeFile; | 2369 | return e_writeFile; |
2370 | } | 2370 | } |
2371 | #endif | 2371 | #endif |
2372 | for (j = 0; j < numEnt; ++j) { | 2372 | for (j = 0; j < numEnt; ++j) { |
2373 | exp = "\n-- "; | 2373 | exp = "\n-- "; |
2374 | exp += dti.dta[i].d[j].desc; | 2374 | exp += dti.dta[i].d[j].desc; |
2375 | exp += " --\n"; | 2375 | exp += " --\n"; |
2376 | 2376 | ||
2377 | exp += i18n("Username: ").latin1(); | 2377 | exp += i18n("Username: ").latin1(); |
2378 | exp += dti.dta[i].d[j].name; | 2378 | exp += dti.dta[i].d[j].name; |
2379 | exp += "\n"; | 2379 | exp += "\n"; |
2380 | 2380 | ||
2381 | exp += i18n("Password: ").latin1(); | 2381 | exp += i18n("Password: ").latin1(); |
2382 | exp += dti.dta[i].d[j].pw; | 2382 | exp += dti.dta[i].d[j].pw; |
2383 | exp += "\n"; | 2383 | exp += "\n"; |
2384 | 2384 | ||
2385 | exp += i18n("Comment: ").latin1(); | 2385 | exp += i18n("Comment: ").latin1(); |
2386 | exp += dti.dta[i].d[j].comment; | 2386 | exp += dti.dta[i].d[j].comment; |
2387 | exp += "\n"; | 2387 | exp += "\n"; |
2388 | 2388 | ||
2389 | exp += i18n("URL: ").latin1(); | 2389 | exp += i18n("URL: ").latin1(); |
2390 | exp += dti.dta[i].d[j].url; | 2390 | exp += dti.dta[i].d[j].url; |
2391 | exp += "\n"; | 2391 | exp += "\n"; |
2392 | 2392 | ||
2393 | exp += i18n("Launcher: ").latin1(); | 2393 | exp += i18n("Launcher: ").latin1(); |
2394 | exp += dti.dta[i].d[j].launcher; | 2394 | exp += dti.dta[i].d[j].launcher; |
2395 | exp += "\n"; | 2395 | exp += "\n"; |
2396 | 2396 | ||
2397 | #ifndef PWM_EMBEDDED | 2397 | #ifndef PWM_EMBEDDED |
2398 | if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) { | 2398 | if (f.writeBlock(exp.c_str(), exp.length()) != (Q_LONG)exp.length()) { |
2399 | unlockAll_tempoary(true); | 2399 | unlockAll_tempoary(true); |
2400 | f.close(); | 2400 | f.close(); |
2401 | return e_writeFile; | 2401 | return e_writeFile; |
2402 | } | 2402 | } |
2403 | #else | 2403 | #else |
2404 | if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) { | 2404 | if (f.writeBlock(exp.c_str(), exp.length()) != (long)exp.length()) { |
2405 | unlockAll_tempoary(true); | 2405 | unlockAll_tempoary(true); |
2406 | f.close(); | 2406 | f.close(); |
2407 | return e_writeFile; | 2407 | return e_writeFile; |
2408 | } | 2408 | } |
2409 | #endif | 2409 | #endif |
2410 | } | 2410 | } |
2411 | } | 2411 | } |
2412 | unlockAll_tempoary(true); | 2412 | unlockAll_tempoary(true); |
2413 | f.close(); | 2413 | f.close(); |
2414 | 2414 | ||
2415 | return e_success; | 2415 | return e_success; |
2416 | } | 2416 | } |
2417 | 2417 | ||
2418 | PwMerror PwMDoc::importFromText(const QString *file, int format) | 2418 | PwMerror PwMDoc::importFromText(const QString *file, int format) |
2419 | { | 2419 | { |
2420 | PWM_ASSERT(file); | 2420 | PWM_ASSERT(file); |
2421 | if (format == 0) | 2421 | if (format == 0) |
2422 | return importText_PwM(file); | 2422 | return importText_PwM(file); |
2423 | else if (format == -1) { | 2423 | else if (format == -1) { |
2424 | // probe for all formats | 2424 | // probe for all formats |
2425 | if (importText_PwM(file) == e_success) | 2425 | if (importText_PwM(file) == e_success) |
2426 | return e_success; | 2426 | return e_success; |
2427 | dti.clear(); | 2427 | dti.clear(); |
2428 | emitDataChanged(this); | 2428 | emitDataChanged(this); |
2429 | // add next format here... | 2429 | // add next format here... |
2430 | return e_fileFormat; | 2430 | return e_fileFormat; |
2431 | } | 2431 | } |
2432 | return e_invalidArg; | 2432 | return e_invalidArg; |
2433 | } | 2433 | } |
2434 | 2434 | ||
2435 | PwMerror PwMDoc::importText_PwM(const QString *file) | 2435 | PwMerror PwMDoc::importText_PwM(const QString *file) |
2436 | { | 2436 | { |
2437 | PWM_ASSERT(file); | 2437 | PWM_ASSERT(file); |
2438 | FILE *f; | 2438 | FILE *f; |
2439 | int tmp; | 2439 | int tmp; |
2440 | ssize_t ret; | 2440 | ssize_t ret; |
2441 | string curCat; | 2441 | string curCat; |
2442 | unsigned int entriesRead = 0; | 2442 | unsigned int entriesRead = 0; |
2443 | PwMDataItem currItem; | 2443 | PwMDataItem currItem; |
2444 | f = fopen(file->latin1(), "r"); | 2444 | f = fopen(file->latin1(), "r"); |
2445 | if (!f) | 2445 | if (!f) |
2446 | return e_openFile; | 2446 | return e_openFile; |
2447 | size_t ch_tmp_size = 1024; | 2447 | size_t ch_tmp_size = 1024; |
2448 | char *ch_tmp = (char*)malloc(ch_tmp_size); | 2448 | char *ch_tmp = (char*)malloc(ch_tmp_size); |
2449 | if (!ch_tmp) { | 2449 | if (!ch_tmp) { |
2450 | fclose(f); | 2450 | fclose(f); |
2451 | return e_outOfMem; | 2451 | return e_outOfMem; |
2452 | } | 2452 | } |
2453 | 2453 | ||
2454 | // - check header | 2454 | // - check header |
2455 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) // skip first line. | 2455 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) // skip first line. |
2456 | goto formatError; | 2456 | goto formatError; |
2457 | // check version-string and return version in "ch_tmp". | 2457 | // check version-string and return version in "ch_tmp". |
2458 | if (fscanf(f, "PwM v%s", ch_tmp) != 1) { | 2458 | if (fscanf(f, "PwM v%s", ch_tmp) != 1) { |
2459 | // header not recognized as PwM generated header | 2459 | // header not recognized as PwM generated header |
2460 | goto formatError; | 2460 | goto formatError; |
2461 | } | 2461 | } |
2462 | // set filepointer behind version-string-line previously checked | 2462 | // set filepointer behind version-string-line previously checked |
2463 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) | 2463 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) |
2464 | goto formatError; | 2464 | goto formatError; |
2465 | // skip next line containing the build-date | 2465 | // skip next line containing the build-date |
2466 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) | 2466 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) |
2467 | goto formatError; | 2467 | goto formatError; |
2468 | // read header termination line | 2468 | // read header termination line |
2469 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) | 2469 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) |
2470 | goto formatError; | 2470 | goto formatError; |
2471 | if (strcmp(ch_tmp, "==============================\n")) | 2471 | if (strcmp(ch_tmp, "==============================\n")) |
2472 | goto formatError; | 2472 | goto formatError; |
2473 | 2473 | ||
2474 | // - read entries | 2474 | // - read entries |
2475 | do { | 2475 | do { |
2476 | // find beginning of next category | 2476 | // find beginning of next category |
2477 | do { | 2477 | do { |
2478 | tmp = fgetc(f); | 2478 | tmp = fgetc(f); |
2479 | } while (tmp == '\n' && tmp != EOF); | 2479 | } while (tmp == '\n' && tmp != EOF); |
2480 | if (tmp == EOF) | 2480 | if (tmp == EOF) |
2481 | break; | 2481 | break; |
2482 | 2482 | ||
2483 | // decrement filepos by one | 2483 | // decrement filepos by one |
2484 | fseek(f, -1, SEEK_CUR); | 2484 | fseek(f, -1, SEEK_CUR); |
2485 | // read cat-name | 2485 | // read cat-name |
2486 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) | 2486 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) |
2487 | goto formatError; | 2487 | goto formatError; |
2488 | // check cat-name format | 2488 | // check cat-name format |
2489 | if (memcmp(ch_tmp, "== Category: ", 13) != 0) | 2489 | if (memcmp(ch_tmp, "== Category: ", 13) != 0) |
2490 | goto formatError; | 2490 | goto formatError; |
2491 | if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " ==", 3) != 0) | 2491 | if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " ==", 3) != 0) |
2492 | goto formatError; | 2492 | goto formatError; |
2493 | // copy cat-name | 2493 | // copy cat-name |
2494 | curCat.assign(ch_tmp + 13, strlen(ch_tmp) - 1 - 16); | 2494 | curCat.assign(ch_tmp + 13, strlen(ch_tmp) - 1 - 16); |
2495 | 2495 | ||
2496 | do { | 2496 | do { |
2497 | // find beginning of next entry | 2497 | // find beginning of next entry |
2498 | do { | 2498 | do { |
2499 | tmp = fgetc(f); | 2499 | tmp = fgetc(f); |
2500 | } while (tmp == '\n' && tmp != EOF && tmp != '='); | 2500 | } while (tmp == '\n' && tmp != EOF && tmp != '='); |
2501 | if (tmp == EOF) | 2501 | if (tmp == EOF) |
2502 | break; | 2502 | break; |
2503 | if (tmp == '=') { | 2503 | if (tmp == '=') { |
2504 | fseek(f, -1, SEEK_CUR); | 2504 | fseek(f, -1, SEEK_CUR); |
2505 | break; | 2505 | break; |
2506 | } | 2506 | } |
2507 | // decrement filepos by one | 2507 | // decrement filepos by one |
2508 | fseek(f, -1, SEEK_CUR); | 2508 | fseek(f, -1, SEEK_CUR); |
2509 | // read desc-line | 2509 | // read desc-line |
2510 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) | 2510 | if (getline(&ch_tmp, &ch_tmp_size, f) == -1) |
2511 | goto formatError; | 2511 | goto formatError; |
2512 | // check desc-line format | 2512 | // check desc-line format |
2513 | if (memcmp(ch_tmp, "-- ", 3) != 0) | 2513 | if (memcmp(ch_tmp, "-- ", 3) != 0) |
2514 | goto formatError; | 2514 | goto formatError; |
2515 | if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " --", 3) != 0) | 2515 | if (memcmp(ch_tmp + (strlen(ch_tmp) - 1 - 3), " --", 3) != 0) |
2516 | goto formatError; | 2516 | goto formatError; |
2517 | // add desc-line | 2517 | // add desc-line |
2518 | currItem.desc.assign(ch_tmp + 3, strlen(ch_tmp) - 1 - 6); | 2518 | currItem.desc.assign(ch_tmp + 3, strlen(ch_tmp) - 1 - 6); |
2519 | 2519 | ||
2520 | // read username-line | 2520 | // read username-line |
2521 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) | 2521 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) |
2522 | goto formatError; | 2522 | goto formatError; |
2523 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.name)) | 2523 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.name)) |
2524 | goto formatError; | 2524 | goto formatError; |
2525 | 2525 | ||
2526 | // read pw-line | 2526 | // read pw-line |
2527 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) | 2527 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) |
2528 | goto formatError; | 2528 | goto formatError; |
2529 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.pw)) | 2529 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.pw)) |
2530 | goto formatError; | 2530 | goto formatError; |
2531 | 2531 | ||
2532 | // read comment-line | 2532 | // read comment-line |
2533 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) | 2533 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) |
2534 | goto formatError; | 2534 | goto formatError; |
2535 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.comment)) | 2535 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.comment)) |
2536 | goto formatError; | 2536 | goto formatError; |
2537 | 2537 | ||
2538 | // read URL-line | 2538 | // read URL-line |
2539 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) | 2539 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) |
2540 | goto formatError; | 2540 | goto formatError; |
2541 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.url)) | 2541 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.url)) |
2542 | goto formatError; | 2542 | goto formatError; |
2543 | 2543 | ||
2544 | // read launcher-line | 2544 | // read launcher-line |
2545 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) | 2545 | if ((ret = getline(&ch_tmp, &ch_tmp_size, f)) == -1) |
2546 | goto formatError; | 2546 | goto formatError; |
2547 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.launcher)) | 2547 | if (!textExtractEntry_PwM(ch_tmp, ret, &currItem.launcher)) |
2548 | goto formatError; | 2548 | goto formatError; |
2549 | 2549 | ||
2550 | currItem.lockStat = true; | 2550 | currItem.lockStat = true; |
2551 | currItem.listViewPos = -1; | 2551 | currItem.listViewPos = -1; |
2552 | addEntry(curCat.c_str(), &currItem, true); | 2552 | addEntry(curCat.c_str(), &currItem, true); |
2553 | ++entriesRead; | 2553 | ++entriesRead; |
2554 | } while (1); | 2554 | } while (1); |
2555 | } while (1); | 2555 | } while (1); |
2556 | if (!entriesRead) | 2556 | if (!entriesRead) |
2557 | goto formatError; | 2557 | goto formatError; |
2558 | 2558 | ||
2559 | free(ch_tmp); | 2559 | free(ch_tmp); |
2560 | fclose(f); | 2560 | fclose(f); |
2561 | flagDirty(); | 2561 | flagDirty(); |
2562 | return e_success; | 2562 | return e_success; |
2563 | 2563 | ||
2564 | formatError: | 2564 | formatError: |
2565 | free(ch_tmp); | 2565 | free(ch_tmp); |
2566 | fclose(f); | 2566 | fclose(f); |
2567 | return e_fileFormat; | 2567 | return e_fileFormat; |
2568 | } | 2568 | } |
2569 | 2569 | ||
2570 | bool PwMDoc::textExtractEntry_PwM(const char *in, ssize_t in_size, string *out) | 2570 | bool PwMDoc::textExtractEntry_PwM(const char *in, ssize_t in_size, string *out) |
2571 | { | 2571 | { |
2572 | PWM_ASSERT(in && out); | 2572 | PWM_ASSERT(in && out); |
2573 | ssize_t i = 0, len = in_size - 1; | 2573 | ssize_t i = 0, len = in_size - 1; |
2574 | while (i < len) { | 2574 | while (i < len) { |
2575 | if (in[i] == ':') | 2575 | if (in[i] == ':') |
2576 | break; | 2576 | break; |
2577 | ++i; | 2577 | ++i; |
2578 | } | 2578 | } |
2579 | i += 2; | 2579 | i += 2; |
2580 | *out = ""; | 2580 | *out = ""; |
2581 | out->append(in + i, in_size - i - 1); | 2581 | out->append(in + i, in_size - i - 1); |
2582 | return true; | 2582 | return true; |
2583 | } | 2583 | } |
2584 | 2584 | ||
2585 | PwMerror PwMDoc::exportToGpasman(const QString *file) | 2585 | PwMerror PwMDoc::exportToGpasman(const QString *file) |
2586 | { | 2586 | { |
2587 | PWM_ASSERT(file); | 2587 | PWM_ASSERT(file); |
2588 | GpasmanFile gp; | 2588 | GpasmanFile gp; |
2589 | int ret; | 2589 | int ret; |
2590 | 2590 | ||
2591 | if (!unlockAll_tempoary()) | 2591 | if (!unlockAll_tempoary()) |
2592 | return e_lock; | 2592 | return e_lock; |
2593 | 2593 | ||
2594 | QString gpmPassword; | 2594 | QString gpmPassword; |
2595 | while (1) { | 2595 | while (1) { |
2596 | gpmPassword = requestNewMpw(0); | 2596 | gpmPassword = requestNewMpw(0); |
2597 | if (gpmPassword == "") { | 2597 | if (gpmPassword == "") { |
2598 | unlockAll_tempoary(true); | 2598 | unlockAll_tempoary(true); |
2599 | return e_noPw; | 2599 | return e_noPw; |
2600 | } | 2600 | } |
2601 | if (gpmPassword.length() < 4) { | 2601 | if (gpmPassword.length() < 4) { |
2602 | gpmPwLenErrMsgBox(); | 2602 | gpmPwLenErrMsgBox(); |
2603 | } else { | 2603 | } else { |
2604 | break; | 2604 | break; |
2605 | } | 2605 | } |
2606 | } | 2606 | } |
2607 | 2607 | ||
2608 | ret = gp.save_init(file->latin1(), gpmPassword.latin1()); | 2608 | ret = gp.save_init(file->latin1(), gpmPassword.latin1()); |
2609 | if (ret != 1) { | 2609 | if (ret != 1) { |
2610 | unlockAll_tempoary(true); | 2610 | unlockAll_tempoary(true); |
2611 | return e_accessFile; | 2611 | return e_accessFile; |
2612 | } | 2612 | } |
2613 | 2613 | ||
2614 | char *entry[4]; | 2614 | char *entry[4]; |
2615 | unsigned int numCat = numCategories(), i; | 2615 | unsigned int numCat = numCategories(), i; |
2616 | unsigned int numEntr, j; | 2616 | unsigned int numEntr, j; |
2617 | int descLen, nameLen, pwLen, commentLen; | 2617 | int descLen, nameLen, pwLen, commentLen; |
2618 | for (i = 0; i < numCat; ++i) { | 2618 | for (i = 0; i < numCat; ++i) { |
2619 | numEntr = numEntries(i); | 2619 | numEntr = numEntries(i); |
2620 | for (j = 0; j < numEntr; ++j) { | 2620 | for (j = 0; j < numEntr; ++j) { |
2621 | descLen = dti.dta[i].d[j].desc.length(); | 2621 | descLen = dti.dta[i].d[j].desc.length(); |
2622 | nameLen = dti.dta[i].d[j].name.length(); | 2622 | nameLen = dti.dta[i].d[j].name.length(); |
2623 | pwLen = dti.dta[i].d[j].pw.length(); | 2623 | pwLen = dti.dta[i].d[j].pw.length(); |
2624 | commentLen = dti.dta[i].d[j].comment.length(); | 2624 | commentLen = dti.dta[i].d[j].comment.length(); |
2625 | entry[0] = new char[descLen + 1]; | 2625 | entry[0] = new char[descLen + 1]; |
2626 | entry[1] = new char[nameLen + 1]; | 2626 | entry[1] = new char[nameLen + 1]; |
2627 | entry[2] = new char[pwLen + 1]; | 2627 | entry[2] = new char[pwLen + 1]; |
2628 | entry[3] = new char[commentLen + 1]; | 2628 | entry[3] = new char[commentLen + 1]; |
2629 | strcpy(entry[0], descLen == 0 ? " " : dti.dta[i].d[j].desc.c_str()); | 2629 | strcpy(entry[0], descLen == 0 ? " " : dti.dta[i].d[j].desc.c_str()); |
2630 | strcpy(entry[1], nameLen == 0 ? " " : dti.dta[i].d[j].name.c_str()); | 2630 | strcpy(entry[1], nameLen == 0 ? " " : dti.dta[i].d[j].name.c_str()); |
2631 | strcpy(entry[2], pwLen == 0 ? " " : dti.dta[i].d[j].pw.c_str()); | 2631 | strcpy(entry[2], pwLen == 0 ? " " : dti.dta[i].d[j].pw.c_str()); |
2632 | strcpy(entry[3], commentLen == 0 ? " " : dti.dta[i].d[j].comment.c_str()); | 2632 | strcpy(entry[3], commentLen == 0 ? " " : dti.dta[i].d[j].comment.c_str()); |
2633 | entry[0][descLen == 0 ? descLen + 1 : descLen] = '\0'; | 2633 | entry[0][descLen == 0 ? descLen + 1 : descLen] = '\0'; |
2634 | entry[1][nameLen == 0 ? nameLen + 1 : nameLen] = '\0'; | 2634 | entry[1][nameLen == 0 ? nameLen + 1 : nameLen] = '\0'; |
2635 | entry[2][pwLen == 0 ? pwLen + 1 : pwLen] = '\0'; | 2635 | entry[2][pwLen == 0 ? pwLen + 1 : pwLen] = '\0'; |
2636 | entry[3][commentLen == 0 ? commentLen + 1 : commentLen] = '\0'; | 2636 | entry[3][commentLen == 0 ? commentLen + 1 : commentLen] = '\0'; |
2637 | 2637 | ||
2638 | ret = gp.save_entry(entry); | 2638 | ret = gp.save_entry(entry); |
2639 | if (ret == -1){ | 2639 | if (ret == -1){ |
2640 | delete [] entry[0]; | 2640 | delete [] entry[0]; |
2641 | delete [] entry[1]; | 2641 | delete [] entry[1]; |
2642 | delete [] entry[2]; | 2642 | delete [] entry[2]; |
2643 | delete [] entry[3]; | 2643 | delete [] entry[3]; |
2644 | gp.save_finalize(); | 2644 | gp.save_finalize(); |
2645 | unlockAll_tempoary(true); | 2645 | unlockAll_tempoary(true); |
2646 | return e_writeFile; | 2646 | return e_writeFile; |
2647 | } | 2647 | } |
2648 | 2648 | ||
2649 | delete [] entry[0]; | 2649 | delete [] entry[0]; |
2650 | delete [] entry[1]; | 2650 | delete [] entry[1]; |
2651 | delete [] entry[2]; | 2651 | delete [] entry[2]; |
2652 | delete [] entry[3]; | 2652 | delete [] entry[3]; |
2653 | } | 2653 | } |
2654 | } | 2654 | } |
2655 | unlockAll_tempoary(true); | 2655 | unlockAll_tempoary(true); |
2656 | if (gp.save_finalize() == -1) | 2656 | if (gp.save_finalize() == -1) |
2657 | return e_writeFile; | 2657 | return e_writeFile; |
2658 | 2658 | ||
2659 | return e_success; | 2659 | return e_success; |
2660 | } | 2660 | } |
2661 | 2661 | ||
2662 | PwMerror PwMDoc::importFromGpasman(const QString *file) | 2662 | PwMerror PwMDoc::importFromGpasman(const QString *file) |
2663 | { | 2663 | { |
2664 | PWM_ASSERT(file); | 2664 | PWM_ASSERT(file); |
2665 | QString pw = requestMpw(false); | 2665 | QString pw = requestMpw(false); |
2666 | if (pw == "") | 2666 | if (pw == "") |
2667 | return e_noPw; | 2667 | return e_noPw; |
2668 | GpasmanFile gp; | 2668 | GpasmanFile gp; |
2669 | int ret, i; | 2669 | int ret, i; |
2670 | PwMerror ret2; | 2670 | PwMerror ret2; |
2671 | char *entry[4]; | 2671 | char *entry[4]; |
2672 | PwMDataItem tmpData; | 2672 | PwMDataItem tmpData; |
2673 | ret = gp.load_init(file->latin1(), pw.latin1()); | 2673 | ret = gp.load_init(file->latin1(), pw.latin1()); |
2674 | if (ret != 1) | 2674 | if (ret != 1) |
2675 | return e_accessFile; | 2675 | return e_accessFile; |
2676 | 2676 | ||
2677 | do { | 2677 | do { |
2678 | ret = gp.load_entry(entry); | 2678 | ret = gp.load_entry(entry); |
2679 | if(ret != 1) | 2679 | if(ret != 1) |
2680 | break; | 2680 | break; |
2681 | tmpData.desc = entry[0]; | 2681 | tmpData.desc = entry[0]; |
2682 | tmpData.name = entry[1]; | 2682 | tmpData.name = entry[1]; |
2683 | tmpData.pw = entry[2]; | 2683 | tmpData.pw = entry[2]; |
2684 | tmpData.comment = entry[3]; | 2684 | tmpData.comment = entry[3]; |
2685 | tmpData.lockStat = true; | 2685 | tmpData.lockStat = true; |
2686 | tmpData.listViewPos = -1; | 2686 | tmpData.listViewPos = -1; |
2687 | ret2 = addEntry(DEFAULT_CATEGORY, &tmpData, true); | 2687 | ret2 = addEntry(DEFAULT_CATEGORY, &tmpData, true); |
2688 | for (i = 0; i < 4; ++i) | 2688 | for (i = 0; i < 4; ++i) |
2689 | free(entry[i]); | 2689 | free(entry[i]); |
2690 | if (ret2 == e_maxAllowedEntr) { | 2690 | if (ret2 == e_maxAllowedEntr) { |
2691 | gp.load_finalize(); | 2691 | gp.load_finalize(); |
2692 | return e_maxAllowedEntr; | 2692 | return e_maxAllowedEntr; |
2693 | } | 2693 | } |
2694 | } while (1); | 2694 | } while (1); |
2695 | gp.load_finalize(); | 2695 | gp.load_finalize(); |
2696 | if (isDocEmpty()) | 2696 | if (isDocEmpty()) |
2697 | return e_wrongPw; // we assume this. | 2697 | return e_wrongPw; // we assume this. |
2698 | 2698 | ||
2699 | flagDirty(); | 2699 | flagDirty(); |
2700 | return e_success; | 2700 | return e_success; |
2701 | } | 2701 | } |
2702 | 2702 | ||
2703 | void PwMDoc::ensureLvp() | 2703 | void PwMDoc::ensureLvp() |
2704 | { | 2704 | { |
2705 | if (isDocEmpty()) | 2705 | if (isDocEmpty()) |
2706 | return; | 2706 | return; |
2707 | 2707 | ||
2708 | vector< vector<PwMDataItem>::iterator > undefined; | 2708 | vector< vector<PwMDataItem>::iterator > undefined; |
2709 | vector< vector<PwMDataItem>::iterator >::iterator undefBegin, | 2709 | vector< vector<PwMDataItem>::iterator >::iterator undefBegin, |
2710 | undefEnd, | 2710 | undefEnd, |
2711 | undefI; | 2711 | undefI; |
2712 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), | 2712 | vector<PwMCategoryItem>::iterator catBegin = dti.dta.begin(), |
2713 | catEnd = dti.dta.end(), | 2713 | catEnd = dti.dta.end(), |
2714 | catI = catBegin; | 2714 | catI = catBegin; |
2715 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; | 2715 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; |
2716 | int lvpTop, tmpLvp; | 2716 | int lvpTop, tmpLvp; |
2717 | 2717 | ||
2718 | while (catI != catEnd) { | 2718 | while (catI != catEnd) { |
2719 | lvpTop = -1; | 2719 | lvpTop = -1; |
2720 | undefined.clear(); | 2720 | undefined.clear(); |
2721 | 2721 | ||
2722 | entrBegin = catI->d.begin(); | 2722 | entrBegin = catI->d.begin(); |
2723 | entrEnd = catI->d.end(); | 2723 | entrEnd = catI->d.end(); |
2724 | entrI = entrBegin; | 2724 | entrI = entrBegin; |
2725 | 2725 | ||
2726 | while (entrI != entrEnd) { | 2726 | while (entrI != entrEnd) { |
2727 | tmpLvp = entrI->listViewPos; | 2727 | tmpLvp = entrI->listViewPos; |
2728 | if (tmpLvp == -1) | 2728 | if (tmpLvp == -1) |
2729 | undefined.push_back(entrI); | 2729 | undefined.push_back(entrI); |
2730 | else if (tmpLvp > lvpTop) | 2730 | else if (tmpLvp > lvpTop) |
2731 | lvpTop = tmpLvp; | 2731 | lvpTop = tmpLvp; |
2732 | ++entrI; | 2732 | ++entrI; |
2733 | } | 2733 | } |
2734 | undefBegin = undefined.begin(); | 2734 | undefBegin = undefined.begin(); |
2735 | undefEnd = undefined.end(); | 2735 | undefEnd = undefined.end(); |
2736 | undefI = undefBegin; | 2736 | undefI = undefBegin; |
2737 | while (undefI != undefEnd) { | 2737 | while (undefI != undefEnd) { |
2738 | (*undefI)->listViewPos = ++lvpTop; | 2738 | (*undefI)->listViewPos = ++lvpTop; |
2739 | ++undefI; | 2739 | ++undefI; |
2740 | } | 2740 | } |
2741 | ++catI; | 2741 | ++catI; |
2742 | } | 2742 | } |
2743 | } | 2743 | } |
2744 | 2744 | ||
2745 | QString PwMDoc::getTitle() | 2745 | QString PwMDoc::getTitle() |
2746 | { | 2746 | { |
2747 | /* NOTE: We have to ensure, that the returned title | 2747 | /* NOTE: We have to ensure, that the returned title |
2748 | * is unique and not reused somewhere else while | 2748 | * is unique and not reused somewhere else while |
2749 | * this document is valid (open). | 2749 | * this document is valid (open). |
2750 | */ | 2750 | */ |
2751 | QString title(getFilename()); | 2751 | QString title(getFilename()); |
2752 | if (title.isEmpty()) { | 2752 | if (title.isEmpty()) { |
2753 | if (unnamedNum == 0) { | 2753 | if (unnamedNum == 0) { |
2754 | unnamedNum = PwMDocList::getNewUnnamedNumber(); | 2754 | unnamedNum = PwMDocList::getNewUnnamedNumber(); |
2755 | PWM_ASSERT(unnamedNum != 0); | 2755 | PWM_ASSERT(unnamedNum != 0); |
2756 | } | 2756 | } |
2757 | title = DEFAULT_TITLE; | 2757 | title = DEFAULT_TITLE; |
2758 | title += " "; | 2758 | title += " "; |
2759 | title += tostr(unnamedNum).c_str(); | 2759 | title += tostr(unnamedNum).c_str(); |
2760 | } | 2760 | } |
2761 | return title; | 2761 | return title; |
2762 | } | 2762 | } |
2763 | 2763 | ||
2764 | bool PwMDoc::tryDelete() | 2764 | bool PwMDoc::tryDelete() |
2765 | { | 2765 | { |
2766 | if (deleted) | 2766 | if (deleted) |
2767 | return true; | 2767 | return true; |
2768 | int ret; | 2768 | int ret; |
2769 | if (isDirty()) { | 2769 | if (isDirty()) { |
2770 | ret = dirtyAskSave(getTitle()); | 2770 | ret = dirtyAskSave(getTitle()); |
2771 | if (ret == 0) { // save to disk | 2771 | if (ret == 0) { // save to disk |
2772 | if (!saveDocUi(this)) | 2772 | if (!saveDocUi(this)) |
2773 | goto out_ignore; | 2773 | goto out_ignore; |
2774 | } else if (ret == 1) { // don't save and delete | 2774 | } else if (ret == 1) { // don't save and delete |
2775 | goto out_accept; | 2775 | goto out_accept; |
2776 | } else { // cancel operation | 2776 | } else { // cancel operation |
2777 | goto out_ignore; | 2777 | goto out_ignore; |
2778 | } | 2778 | } |
2779 | } | 2779 | } |
2780 | out_accept: | 2780 | out_accept: |
2781 | deleted = true; | 2781 | deleted = true; |
2782 | delete this; | 2782 | delete this; |
2783 | return true; | 2783 | return true; |
2784 | out_ignore: | 2784 | out_ignore: |
2785 | return false; | 2785 | return false; |
2786 | } | 2786 | } |
2787 | 2787 | ||
2788 | 2788 | ||
2789 | 2789 | ||
2790 | #ifdef PWM_EMBEDDED | 2790 | #ifdef PWM_EMBEDDED |
2791 | //US ENH: this is the magic function that syncronizes the this doc with the remote doc | 2791 | //US ENH: this is the magic function that syncronizes the this doc with the remote doc |
2792 | //US it could have been defined as static, but I did not want to. | 2792 | //US it could have been defined as static, but I did not want to. |
2793 | PwMerror PwMDoc::syncronize(KSyncManager* manager, PwMDoc* syncLocal , PwMDoc* syncRemote, int mode ) | 2793 | PwMerror PwMDoc::syncronize(KSyncManager* manager, PwMDoc* syncLocal , PwMDoc* syncRemote, int mode ) |
2794 | { | 2794 | { |
2795 | int addedPasswordsLocal = 0; | 2795 | int addedPasswordsLocal = 0; |
2796 | int addedPasswordsRemote = 0; | 2796 | int addedPasswordsRemote = 0; |
2797 | int deletedPasswordsRemote = 0; | 2797 | int deletedPasswordsRemote = 0; |
2798 | int deletedPasswordsLocal = 0; | 2798 | int deletedPasswordsLocal = 0; |
2799 | int changedLocal = 0; | 2799 | int changedLocal = 0; |
2800 | int changedRemote = 0; | 2800 | int changedRemote = 0; |
2801 | 2801 | ||
2802 | PwMSyncItem* syncItemLocal; | 2802 | PwMSyncItem* syncItemLocal; |
2803 | PwMSyncItem* syncItemRemote; | 2803 | PwMSyncItem* syncItemRemote; |
2804 | 2804 | ||
2805 | QString mCurrentSyncName = manager->getCurrentSyncName(); | 2805 | QString mCurrentSyncName = manager->getCurrentSyncName(); |
2806 | QString mCurrentSyncDevice = manager->getCurrentSyncDevice(); | 2806 | QString mCurrentSyncDevice = manager->getCurrentSyncDevice(); |
2807 | 2807 | ||
2808 | bool fullDateRange = false; | 2808 | bool fullDateRange = false; |
2809 | int take; | 2809 | int take; |
2810 | // local->resetTempSyncStat(); | 2810 | // local->resetTempSyncStat(); |
2811 | QDateTime mLastSync = QDateTime::currentDateTime(); | 2811 | QDateTime mLastSync = QDateTime::currentDateTime(); |
2812 | QDateTime modifiedSync = mLastSync; | 2812 | QDateTime modifiedSync = mLastSync; |
2813 | 2813 | ||
2814 | unsigned int index; | 2814 | unsigned int index; |
2815 | //Step 1. Find syncinfo in Local file and create if not existent. | 2815 | //Step 1. Find syncinfo in Local file and create if not existent. |
2816 | bool found = syncLocal->findSyncData(mCurrentSyncDevice, &index); | 2816 | bool found = syncLocal->findSyncData(mCurrentSyncDevice, &index); |
2817 | if (found == false) | 2817 | if (found == false) |
2818 | { | 2818 | { |
2819 | PwMSyncItem newSyncItemLocal; | 2819 | PwMSyncItem newSyncItemLocal; |
2820 | newSyncItemLocal.syncName = mCurrentSyncDevice; | 2820 | newSyncItemLocal.syncName = mCurrentSyncDevice; |
2821 | newSyncItemLocal.lastSyncDate = mLastSync; | 2821 | newSyncItemLocal.lastSyncDate = mLastSync; |
2822 | syncLocal->addSyncDataEntry(&newSyncItemLocal, true); | 2822 | syncLocal->addSyncDataEntry(&newSyncItemLocal, true); |
2823 | found = syncLocal->findSyncData(mCurrentSyncDevice, &index); | 2823 | found = syncLocal->findSyncData(mCurrentSyncDevice, &index); |
2824 | if (found == false) { | 2824 | if (found == false) { |
2825 | qDebug("PwMDoc::syncronize : newly created local sync data could not be found"); | 2825 | qDebug("PwMDoc::syncronize : newly created local sync data could not be found"); |
2826 | return e_syncError; | 2826 | return e_syncError; |
2827 | } | 2827 | } |
2828 | } | 2828 | } |
2829 | 2829 | ||
2830 | syncItemLocal = syncLocal->getSyncDataEntry(index); | 2830 | syncItemLocal = syncLocal->getSyncDataEntry(index); |
2831 | qDebug("Last Sync %s ", syncItemLocal->lastSyncDate.toString().latin1()); | 2831 | qDebug("Last Sync %s ", syncItemLocal->lastSyncDate.toString().latin1()); |
2832 | 2832 | ||
2833 | //Step 2. Find syncinfo in remote file and create if not existent. | 2833 | //Step 2. Find syncinfo in remote file and create if not existent. |
2834 | found = syncRemote->findSyncData(mCurrentSyncName, &index); | 2834 | found = syncRemote->findSyncData(mCurrentSyncName, &index); |
2835 | if (found == false) | 2835 | if (found == false) |
2836 | { | 2836 | { |
2837 | qDebug("FULLDATE 1"); | 2837 | qDebug("FULLDATE 1"); |
2838 | fullDateRange = true; | 2838 | fullDateRange = true; |
2839 | PwMSyncItem newSyncItemRemote; | 2839 | PwMSyncItem newSyncItemRemote; |
2840 | newSyncItemRemote.syncName = mCurrentSyncName; | 2840 | newSyncItemRemote.syncName = mCurrentSyncName; |
2841 | newSyncItemRemote.lastSyncDate = mLastSync; | 2841 | newSyncItemRemote.lastSyncDate = mLastSync; |
2842 | syncRemote->addSyncDataEntry(&newSyncItemRemote, true); | 2842 | syncRemote->addSyncDataEntry(&newSyncItemRemote, true); |
2843 | found = syncRemote->findSyncData(mCurrentSyncName, &index); | 2843 | found = syncRemote->findSyncData(mCurrentSyncName, &index); |
2844 | if (found == false) { | 2844 | if (found == false) { |
2845 | qDebug("PwMDoc::syncronize : newly created remote sync data could not be found"); | 2845 | qDebug("PwMDoc::syncronize : newly created remote sync data could not be found"); |
2846 | return e_syncError; | 2846 | return e_syncError; |
2847 | } | 2847 | } |
2848 | } | 2848 | } |
2849 | 2849 | ||
2850 | syncItemRemote = syncRemote->getSyncDataEntry(index); | 2850 | syncItemRemote = syncRemote->getSyncDataEntry(index); |
2851 | //and remove the found entry here. We will reenter it later again. | 2851 | //and remove the found entry here. We will reenter it later again. |
2852 | syncRemote->delSyncDataEntry(index, true); | 2852 | syncRemote->delSyncDataEntry(index, true); |
2853 | 2853 | ||
2854 | 2854 | ||
2855 | if ( syncItemLocal->lastSyncDate == mLastSync ) { | 2855 | if ( syncItemLocal->lastSyncDate == mLastSync ) { |
2856 | qDebug("FULLDATE 2"); | 2856 | qDebug("FULLDATE 2"); |
2857 | fullDateRange = true; | 2857 | fullDateRange = true; |
2858 | } | 2858 | } |
2859 | 2859 | ||
2860 | if ( ! fullDateRange ) { | 2860 | if ( ! fullDateRange ) { |
2861 | if ( syncItemLocal->lastSyncDate != syncItemRemote->lastSyncDate ) { | 2861 | if ( syncItemLocal->lastSyncDate != syncItemRemote->lastSyncDate ) { |
2862 | 2862 | ||
2863 | // qDebug("set fulldate to true %s %s" ,addresseeLSync->dtStart().toString().latin1(), addresseeRSync->dtStart().toString().latin1() ); | 2863 | // qDebug("set fulldate to true %s %s" ,addresseeLSync->dtStart().toString().latin1(), addresseeRSync->dtStart().toString().latin1() ); |
2864 | //qDebug("%d %d %d %d ", addresseeLSync->dtStart().time().second(), addresseeLSync->dtStart().time().msec() , addresseeRSync->dtStart().time().second(), addresseeRSync->dtStart().time().msec()); | 2864 | //qDebug("%d %d %d %d ", addresseeLSync->dtStart().time().second(), addresseeLSync->dtStart().time().msec() , addresseeRSync->dtStart().time().second(), addresseeRSync->dtStart().time().msec()); |
2865 | fullDateRange = true; | 2865 | fullDateRange = true; |
2866 | qDebug("FULLDATE 3 %s %s", syncItemLocal->lastSyncDate.toString().latin1() , syncItemRemote->lastSyncDate.toString().latin1() ); | 2866 | qDebug("FULLDATE 3 %s %s", syncItemLocal->lastSyncDate.toString().latin1() , syncItemRemote->lastSyncDate.toString().latin1() ); |
2867 | } | 2867 | } |
2868 | } | 2868 | } |
2869 | // fullDateRange = true; // debug only! | 2869 | // fullDateRange = true; // debug only! |
2870 | if ( fullDateRange ) | 2870 | if ( fullDateRange ) |
2871 | mLastSync = QDateTime::currentDateTime().addDays( -100*365); | 2871 | mLastSync = QDateTime::currentDateTime().addDays( -100*365); |
2872 | else | 2872 | else |
2873 | mLastSync = syncItemLocal->lastSyncDate; | 2873 | mLastSync = syncItemLocal->lastSyncDate; |
2874 | 2874 | ||
2875 | 2875 | ||
2876 | qDebug("*************************** "); | 2876 | qDebug("*************************** "); |
2877 | // qDebug("mLastAddressbookSync %s ",mLastAddressbookSync.toString().latin1() ); | 2877 | // qDebug("mLastAddressbookSync %s ",mLastAddressbookSync.toString().latin1() ); |
2878 | QStringList er = syncRemote->getIDEntryList(); | 2878 | QStringList er = syncRemote->getIDEntryList(); |
2879 | PwMDataItem* inRemote ;//= er.first(); | 2879 | PwMDataItem* inRemote ;//= er.first(); |
2880 | PwMDataItem* inLocal; | 2880 | PwMDataItem* inLocal; |
2881 | unsigned int catLocal, indexLocal; | 2881 | unsigned int catLocal, indexLocal; |
2882 | unsigned int catRemote, indexRemote; | 2882 | unsigned int catRemote, indexRemote; |
2883 | 2883 | ||
2884 | QString uid; | 2884 | QString uid; |
2885 | manager->showProgressBar(0, i18n("Syncing - close to abort!"), er.count()); | 2885 | manager->showProgressBar(0, i18n("Syncing - close to abort!"), er.count()); |
2886 | 2886 | ||
2887 | int modulo = (er.count()/10)+1; | 2887 | int modulo = (er.count()/10)+1; |
2888 | unsigned int incCounter = 0; | 2888 | unsigned int incCounter = 0; |
2889 | while ( incCounter < er.count()) { | 2889 | while ( incCounter < er.count()) { |
2890 | if (manager->isProgressBarCanceled()) | 2890 | if (manager->isProgressBarCanceled()) |
2891 | return e_syncError; | 2891 | return e_syncError; |
2892 | if ( incCounter % modulo == 0 ) | 2892 | if ( incCounter % modulo == 0 ) |
2893 | manager->showProgressBar(incCounter); | 2893 | manager->showProgressBar(incCounter); |
2894 | 2894 | ||
2895 | uid = er[ incCounter ]; | 2895 | uid = er[ incCounter ]; |
2896 | qDebug("sync uid %s from remote file", uid.latin1()); | ||
2897 | |||
2896 | qApp->processEvents(); | 2898 | qApp->processEvents(); |
2897 | 2899 | ||
2898 | inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal ); | 2900 | inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal ); |
2899 | inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote ); | 2901 | inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote ); |
2902 | PWM_ASSERT(inRemote); | ||
2900 | if ( inLocal != 0 ) { // maybe conflict - same uid in both files | 2903 | if ( inLocal != 0 ) { // maybe conflict - same uid in both files |
2901 | if ( (take = takePwMDataItem( inLocal, inRemote, mLastSync, mode, fullDateRange) ) ) { | 2904 | if ( (take = takePwMDataItem( inLocal, inRemote, mLastSync, mode, fullDateRange) ) ) { |
2902 | //qDebug("take %d %s ", take, inL.summary().latin1()); | 2905 | //qDebug("take %d %s ", take, inL.summary().latin1()); |
2903 | if ( take == 3 ) | 2906 | if ( take == 3 ) |
2904 | return e_syncError; | 2907 | return e_syncError; |
2905 | if ( take == 1 ) {// take local | 2908 | if ( take == 1 ) {// take local |
2906 | //US syncRemote->removeAddressee( inRemote ); | 2909 | //US syncRemote->removeAddressee( inRemote ); |
2907 | (*inRemote) = (*inLocal); | 2910 | (*inRemote) = (*inLocal); |
2908 | //US syncRemote->insertAddressee( inRemote , false); | 2911 | //US syncRemote->insertAddressee( inRemote , false); |
2909 | ++changedRemote; | 2912 | ++changedRemote; |
2910 | } else { // take == 2 take remote | 2913 | } else { // take == 2 take remote |
2911 | //US syncLocal->removeAddressee( inLocal ); | 2914 | //US syncLocal->removeAddressee( inLocal ); |
2912 | (*inLocal) = (*inRemote); | 2915 | (*inLocal) = (*inRemote); |
2913 | //US syncLocal->insertAddressee( inLocal , false ); | 2916 | //US syncLocal->insertAddressee( inLocal , false ); |
2914 | ++changedLocal; | 2917 | ++changedLocal; |
2915 | } | 2918 | } |
2916 | } | 2919 | } |
2917 | } else { // no conflict | 2920 | } else { // no conflict |
2918 | if ( inRemote->meta.update > mLastSync || mode == 5 ) { | 2921 | if ( inRemote->meta.update > mLastSync || mode == 5 ) { |
2919 | inRemote->meta.update = modifiedSync; | 2922 | inRemote->meta.update = modifiedSync; |
2920 | //US syncRemote->insertAddressee( inRemote, false ); | 2923 | //US syncRemote->insertAddressee( inRemote, false ); |
2921 | //US syncLocal->insertAddressee( inRemote, false ); | 2924 | //US syncLocal->insertAddressee( inRemote, false ); |
2922 | syncLocal->addEntry("newcategory", inRemote, true, false); | 2925 | syncLocal->addEntry("newcategory", inRemote, true, false); |
2923 | 2926 | ||
2924 | ++addedPasswordsLocal; | 2927 | ++addedPasswordsLocal; |
2925 | } else { | 2928 | } else { |
2926 | // pending checkExternSyncAddressee(addresseeRSyncSharp, inR); | 2929 | // pending checkExternSyncAddressee(addresseeRSyncSharp, inR); |
2927 | syncRemote->delEntry(catRemote, indexRemote, true); | 2930 | syncRemote->delEntry(catRemote, indexRemote, true); |
2928 | //USsyncRemote->removeAddressee( inRemote ); | 2931 | //USsyncRemote->removeAddressee( inRemote ); |
2929 | ++deletedPasswordsRemote; | 2932 | ++deletedPasswordsRemote; |
2930 | } | 2933 | } |
2931 | } | 2934 | } |
2932 | 2935 | ||
2933 | ++incCounter; | 2936 | ++incCounter; |
2934 | } | 2937 | } |
2935 | 2938 | ||
2936 | 2939 | ||
2937 | er.clear(); | 2940 | er.clear(); |
2938 | QStringList el = syncLocal->getIDEntryList(); | 2941 | QStringList el = syncLocal->getIDEntryList(); |
2939 | modulo = (el.count()/10)+1; | 2942 | modulo = (el.count()/10)+1; |
2940 | 2943 | ||
2941 | manager->showProgressBar(0, i18n("Add / remove addressees"), el.count()); | 2944 | manager->showProgressBar(0, i18n("Add / remove addressees"), el.count()); |
2942 | incCounter = 0; | 2945 | incCounter = 0; |
2943 | while ( incCounter < el.count()) { | 2946 | while ( incCounter < el.count()) { |
2944 | qApp->processEvents(); | 2947 | qApp->processEvents(); |
2945 | if (manager->isProgressBarCanceled()) | 2948 | if (manager->isProgressBarCanceled()) |
2946 | return e_syncError; | 2949 | return e_syncError; |
2947 | if ( incCounter % modulo == 0 ) | 2950 | if ( incCounter % modulo == 0 ) |
2948 | manager->showProgressBar(incCounter); | 2951 | manager->showProgressBar(incCounter); |
2949 | uid = el[ incCounter ]; | 2952 | uid = el[ incCounter ]; |
2950 | 2953 | ||
2951 | inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal ); | 2954 | inLocal = syncLocal->findEntryByID( uid, &catLocal, &indexLocal ); |
2952 | inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote ); | 2955 | inRemote = syncRemote->findEntryByID( uid, &catRemote, &indexRemote ); |
2953 | if ( inRemote == 0 ) { | 2956 | if ( inRemote == 0 ) { |
2954 | if ( inLocal->meta.update < mLastSync && mode != 4 ) { | 2957 | if ( inLocal->meta.update < mLastSync && mode != 4 ) { |
2955 | // pending checkExternSyncAddressee(addresseeLSyncSharp, inL); | 2958 | // pending checkExternSyncAddressee(addresseeLSyncSharp, inL); |
2956 | syncLocal->delEntry(catLocal, indexLocal, true); | 2959 | syncLocal->delEntry(catLocal, indexLocal, true); |
2957 | //USsyncLocal->removeAddressee( inLocal ); | 2960 | //USsyncLocal->removeAddressee( inLocal ); |
2958 | ++deletedPasswordsLocal; | 2961 | ++deletedPasswordsLocal; |
2959 | } else { | 2962 | } else { |
2960 | if ( ! PWMPrefs::instance()->mWriteBackExistingOnly ) { | 2963 | if ( ! PWMPrefs::instance()->mWriteBackExistingOnly ) { |
2961 | ++addedPasswordsRemote; | 2964 | ++addedPasswordsRemote; |
2962 | inLocal->meta.update = modifiedSync; | 2965 | inLocal->meta.update = modifiedSync; |
2963 | //USsyncLocal->insertAddressee( inLocal, false ); | 2966 | //USsyncLocal->insertAddressee( inLocal, false ); |
2964 | (*inRemote) = (*inLocal); | 2967 | (*inRemote) = (*inLocal); |
2965 | //USsyncRemote->insertAddressee( inRemote, false ); | 2968 | //USsyncRemote->insertAddressee( inRemote, false ); |
2966 | syncRemote->addEntry("newcategory", inRemote, true, false); | 2969 | syncRemote->addEntry("newcategory", inRemote, true, false); |
2967 | 2970 | ||
2968 | } | 2971 | } |
2969 | } | 2972 | } |
2970 | 2973 | ||
2971 | } | 2974 | } |
2972 | ++incCounter; | 2975 | ++incCounter; |
2973 | } | 2976 | } |
2974 | el.clear(); | 2977 | el.clear(); |
2975 | manager->hideProgressBar(); | 2978 | manager->hideProgressBar(); |
2976 | 2979 | ||
2977 | // Now write the info back into the sync data space of the files | 2980 | // Now write the info back into the sync data space of the files |
2978 | 2981 | ||
2979 | mLastSync = QDateTime::currentDateTime().addSecs( 1 ); | 2982 | mLastSync = QDateTime::currentDateTime().addSecs( 1 ); |
2980 | // get rid of micro seconds | 2983 | // get rid of micro seconds |
2981 | QTime t = mLastSync.time(); | 2984 | QTime t = mLastSync.time(); |
2982 | mLastSync.setTime( QTime (t.hour (), t.minute (), t.second () ) ); | 2985 | mLastSync.setTime( QTime (t.hour (), t.minute (), t.second () ) ); |
2983 | 2986 | ||
2984 | 2987 | ||
2985 | syncItemLocal->lastSyncDate = mLastSync; | 2988 | syncItemLocal->lastSyncDate = mLastSync; |
2986 | syncItemRemote->lastSyncDate = mLastSync; | 2989 | syncItemRemote->lastSyncDate = mLastSync; |
2987 | 2990 | ||
2988 | // addresseeRSync.setRole( i18n("!Remote from: ")+mCurrentSyncName ) ; | 2991 | // addresseeRSync.setRole( i18n("!Remote from: ")+mCurrentSyncName ) ; |
2989 | // addresseeLSync.setRole(i18n("!Local from: ") + mCurrentSyncName ); | 2992 | // addresseeLSync.setRole(i18n("!Local from: ") + mCurrentSyncName ); |
2990 | 2993 | ||
2991 | syncRemote->addSyncDataEntry( syncItemRemote, false ); | 2994 | syncRemote->addSyncDataEntry( syncItemRemote, false ); |
2992 | syncLocal->addSyncDataEntry( syncItemLocal, false ); | 2995 | syncLocal->addSyncDataEntry( syncItemLocal, false ); |
2993 | QString mes; | 2996 | QString mes; |
2994 | 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 ); | 2997 | 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 ); |
2995 | if ( PWMPrefs::instance()->mShowSyncSummary ) { | 2998 | if ( PWMPrefs::instance()->mShowSyncSummary ) { |
2996 | KMessageBox::information(0, mes, i18n("PWM/Pi Synchronization") ); | 2999 | KMessageBox::information(0, mes, i18n("PWM/Pi Synchronization") ); |
2997 | } | 3000 | } |
2998 | qDebug( mes ); | 3001 | qDebug( mes ); |
2999 | return e_success; | 3002 | return e_success; |
3000 | } | 3003 | } |
3001 | 3004 | ||
3002 | 3005 | ||
3003 | int PwMDoc::takePwMDataItem( PwMDataItem* local, PwMDataItem* remote, QDateTime lastSync, int mode , bool full ) | 3006 | int PwMDoc::takePwMDataItem( PwMDataItem* local, PwMDataItem* remote, QDateTime lastSync, int mode , bool full ) |
3004 | { | 3007 | { |
3005 | // 0 equal | 3008 | // 0 equal |
3006 | // 1 take local | 3009 | // 1 take local |
3007 | // 2 take remote | 3010 | // 2 take remote |
3008 | // 3 cancel | 3011 | // 3 cancel |
3009 | QDateTime localMod = local->meta.update; | 3012 | QDateTime localMod = local->meta.update; |
3010 | QDateTime remoteMod = remote->meta.update; | 3013 | QDateTime remoteMod = remote->meta.update; |
3011 | 3014 | ||
3012 | //US QString mCurrentSyncDevice = syncManager->getCurrentSyncDevice(); | 3015 | //US QString mCurrentSyncDevice = syncManager->getCurrentSyncDevice(); |
3013 | 3016 | ||
3014 | if ( localMod == remoteMod ) | 3017 | if ( localMod == remoteMod ) |
3015 | return 0; | 3018 | return 0; |
3016 | 3019 | ||
3017 | qDebug(" %d %d conflict on %s %s ", mode, full, local->desc.c_str(), remote->desc.c_str() ); | 3020 | qDebug(" %d %d conflict on %s %s ", mode, full, local->desc.c_str(), remote->desc.c_str() ); |
3018 | 3021 | ||
3019 | //qDebug("%s %d %s %d", local->lastModified().toString().latin1() , localMod, remote->lastModified().toString().latin1(), remoteMod); | 3022 | //qDebug("%s %d %s %d", local->lastModified().toString().latin1() , localMod, remote->lastModified().toString().latin1(), remoteMod); |
3020 | //qDebug("%d %d %d %d ", local->lastModified().time().second(), local->lastModified().time().msec(), remote->lastModified().time().second(), remote->lastModified().time().msec() ); | 3023 | //qDebug("%d %d %d %d ", local->lastModified().time().second(), local->lastModified().time().msec(), remote->lastModified().time().second(), remote->lastModified().time().msec() ); |
3021 | //full = true; //debug only | 3024 | //full = true; //debug only |
3022 | if ( full ) { | 3025 | if ( full ) { |
3023 | bool equ = true;//US ( (*local) == (*remote) ); | 3026 | bool equ = true;//US ( (*local) == (*remote) ); |
3024 | if ( equ ) { | 3027 | if ( equ ) { |
3025 | //qDebug("equal "); | 3028 | //qDebug("equal "); |
3026 | if ( mode < SYNC_PREF_FORCE_LOCAL ) | 3029 | if ( mode < SYNC_PREF_FORCE_LOCAL ) |
3027 | return 0; | 3030 | return 0; |
3028 | 3031 | ||
3029 | }//else //debug only | 3032 | }//else //debug only |
3030 | //qDebug("not equal %s %s ", local->summary().latin1(), remote->summary().latin1()); | 3033 | //qDebug("not equal %s %s ", local->summary().latin1(), remote->summary().latin1()); |
3031 | } | 3034 | } |
3032 | 3035 | ||
3033 | int result; | 3036 | int result; |
3034 | bool localIsNew; | 3037 | bool localIsNew; |
3035 | //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() ); | 3038 | //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() ); |
3036 | 3039 | ||
3037 | if ( full && mode < SYNC_PREF_NEWEST ) | 3040 | if ( full && mode < SYNC_PREF_NEWEST ) |
3038 | mode = SYNC_PREF_ASK; | 3041 | mode = SYNC_PREF_ASK; |
3039 | 3042 | ||
3040 | switch( mode ) { | 3043 | switch( mode ) { |
3041 | case SYNC_PREF_LOCAL: | 3044 | case SYNC_PREF_LOCAL: |
3042 | if ( lastSync > remoteMod ) | 3045 | if ( lastSync > remoteMod ) |
3043 | return 1; | 3046 | return 1; |
3044 | if ( lastSync > localMod ) | 3047 | if ( lastSync > localMod ) |
3045 | return 2; | 3048 | return 2; |
3046 | return 1; | 3049 | return 1; |
3047 | break; | 3050 | break; |
3048 | case SYNC_PREF_REMOTE: | 3051 | case SYNC_PREF_REMOTE: |
3049 | if ( lastSync > remoteMod ) | 3052 | if ( lastSync > remoteMod ) |
3050 | return 1; | 3053 | return 1; |
3051 | if ( lastSync > localMod ) | 3054 | if ( lastSync > localMod ) |
3052 | return 2; | 3055 | return 2; |
3053 | return 2; | 3056 | return 2; |
3054 | break; | 3057 | break; |
3055 | case SYNC_PREF_NEWEST: | 3058 | case SYNC_PREF_NEWEST: |
3056 | if ( localMod > remoteMod ) | 3059 | if ( localMod > remoteMod ) |
3057 | return 1; | 3060 | return 1; |
3058 | else | 3061 | else |
3059 | return 2; | 3062 | return 2; |
3060 | break; | 3063 | break; |
3061 | case SYNC_PREF_ASK: | 3064 | case SYNC_PREF_ASK: |
3062 | //qDebug("lsy %s --- lo %s --- re %s ", lastSync.toString().latin1(), localMod.toString().latin1(), remoteMod.toString().latin1() ); | 3065 | //qDebug("lsy %s --- lo %s --- re %s ", lastSync.toString().latin1(), localMod.toString().latin1(), remoteMod.toString().latin1() ); |
3063 | if ( lastSync > remoteMod ) | 3066 | if ( lastSync > remoteMod ) |
3064 | return 1; | 3067 | return 1; |
3065 | if ( lastSync > localMod ) | 3068 | if ( lastSync > localMod ) |
3066 | return 2; | 3069 | return 2; |
3067 | localIsNew = localMod >= remoteMod; | 3070 | localIsNew = localMod >= remoteMod; |
3068 | //qDebug("conflict! ************************************** "); | 3071 | //qDebug("conflict! ************************************** "); |
3069 | { | 3072 | { |
3070 | PwMDataItemChooser acd ( *local,*remote, localIsNew , 0/*this*/ ); | 3073 | PwMDataItemChooser acd ( *local,*remote, localIsNew , 0/*this*/ ); |
3071 | result = acd.executeD(localIsNew); | 3074 | result = acd.executeD(localIsNew); |
3072 | return result; | 3075 | return result; |
3073 | } | 3076 | } |
3074 | break; | 3077 | break; |
3075 | case SYNC_PREF_FORCE_LOCAL: | 3078 | case SYNC_PREF_FORCE_LOCAL: |
3076 | return 1; | 3079 | return 1; |
3077 | break; | 3080 | break; |
3078 | case SYNC_PREF_FORCE_REMOTE: | 3081 | case SYNC_PREF_FORCE_REMOTE: |
3079 | return 2; | 3082 | return 2; |
3080 | break; | 3083 | break; |
3081 | 3084 | ||
3082 | default: | 3085 | default: |
3083 | // SYNC_PREF_TAKE_BOTH not implemented | 3086 | // SYNC_PREF_TAKE_BOTH not implemented |
3084 | break; | 3087 | break; |
3085 | } | 3088 | } |
3086 | return 0; | 3089 | return 0; |
3087 | } | 3090 | } |
3088 | 3091 | ||
3089 | 3092 | ||
3090 | 3093 | ||
3091 | 3094 | ||
3092 | //this are the overwritten callbackmethods from the syncinterface | 3095 | //this are the overwritten callbackmethods from the syncinterface |
3093 | bool PwMDoc::sync(KSyncManager* manager, QString filename, int mode) | 3096 | bool PwMDoc::sync(KSyncManager* manager, QString filename, int mode) |
3094 | { | 3097 | { |
3095 | QString mCurrentSyncDevice = manager->getCurrentSyncDevice(); | 3098 | QString mCurrentSyncDevice = manager->getCurrentSyncDevice(); |
3096 | 3099 | ||
3097 | // construct on the stack = automatic cleanup. | 3100 | //1) unlock local file first if necessary (ask for password) |
3101 | if (this->isDeepLocked()) { | ||
3102 | PwMerror ret = this->deepLock(false); | ||
3103 | if (ret != e_success) | ||
3104 | return false; | ||
3105 | } | ||
3106 | |||
3107 | //2) construct and open a new doc on the stack(automatic cleanup) for remote file. | ||
3098 | PwMDoc syncTarget(this, "synctarget"); | 3108 | PwMDoc syncTarget(this, "synctarget"); |
3109 | PwMDoc* pSyncTarget = &syncTarget; | ||
3110 | |||
3099 | 3111 | ||
3100 | PwMerror err = syncTarget.openDoc(&filename, 2 /*== deeplocked*/); | 3112 | PwMerror err = pSyncTarget->openDoc(&filename, 1 /*== open with all entries locked*/); |
3101 | 3113 | ||
3102 | if (err != e_success) | 3114 | if (err == e_alreadyOpen) { |
3115 | PwMDocList::listItem li; | ||
3116 | if (getOpenDocList()->find(filename.latin1(), &li)) | ||
3117 | pSyncTarget = li.doc; | ||
3118 | else { | ||
3119 | qDebug("PwmDoc::sync: sync failed. Error %i while opening file %s",err, filename.latin1()); | ||
3120 | return false; | ||
3121 | } | ||
3122 | } | ||
3123 | else if (err != e_success) { | ||
3124 | qDebug("PwmDoc::sync: sync failed. Error %i while opening file %s",err, filename.latin1()); | ||
3103 | return false; | 3125 | return false; |
3104 | 3126 | } | |
3127 | |||
3105 | qDebug("PWM file loaded %s,sync mode %d",filename.latin1(), mode ); | 3128 | qDebug("PWM file loaded %s,sync mode %d",filename.latin1(), mode ); |
3106 | 3129 | ||
3130 | |||
3131 | //3) unlock remote file first if necessary (ask for password) | ||
3132 | if (pSyncTarget->isDeepLocked()) { | ||
3133 | PwMerror ret = pSyncTarget->deepLock(false); | ||
3134 | if (ret != e_success) | ||
3135 | return false; | ||
3136 | } | ||
3137 | |||
3107 | 3138 | ||
3108 | err = syncronize(manager, this, &syncTarget, mode ); | 3139 | err = syncronize(manager, this, pSyncTarget, mode ); |
3109 | 3140 | ||
3110 | if (err == e_success) { | 3141 | if (err == e_success) { |
3111 | if ( PWMPrefs::instance()->mWriteBackFile ){ | 3142 | if ( PWMPrefs::instance()->mWriteBackFile ){ |
3112 | qDebug("Saving remote PWManager file"); | 3143 | qDebug("Saving remote PWManager file"); |
3113 | err = syncTarget.saveDoc(conf()->confGlobCompression()); | 3144 | err = pSyncTarget->saveDoc(conf()->confGlobCompression()); |
3114 | if (err != e_success) | 3145 | if (err != e_success) { |
3146 | qDebug("PwmDoc::sync: Sync failed. Error %i while storing file %s",err, filename.latin1()); | ||
3115 | return false; | 3147 | return false; |
3116 | 3148 | } | |
3117 | } | 3149 | } |
3118 | 3150 | ||
3119 | flagDirty(); | 3151 | flagDirty(); |
3120 | return true; | 3152 | return true; |
3121 | } | 3153 | } |
3122 | else { | 3154 | else { |
3123 | return false; | 3155 | return false; |
3124 | } | 3156 | } |
3125 | } | 3157 | } |
3126 | 3158 | ||
3127 | //called by the syncmanager to indicate that the work has to marked as dirty. | 3159 | //called by the syncmanager to indicate that the work has to marked as dirty. |
3128 | void PwMDoc::sync_setModified() | 3160 | void PwMDoc::sync_setModified() |
3129 | { | 3161 | { |
3130 | flagDirty(); | 3162 | flagDirty(); |
3131 | } | 3163 | } |
3132 | 3164 | ||
3133 | //called by the syncmanager to ask if the dirty flag is set. | 3165 | //called by the syncmanager to ask if the dirty flag is set. |
3134 | bool PwMDoc::sync_isModified() | 3166 | bool PwMDoc::sync_isModified() |
3135 | { | 3167 | { |
3136 | return isDirty(); | 3168 | return isDirty(); |
3137 | } | 3169 | } |
3138 | 3170 | ||
3139 | //called by the syncmanager to indicate that the work has to be saved. | 3171 | //called by the syncmanager to indicate that the work has to be saved. |
3140 | void PwMDoc::sync_save() | 3172 | void PwMDoc::sync_save() |
3141 | { | 3173 | { |
3142 | saveDoc(conf()->confGlobCompression()); | 3174 | saveDoc(conf()->confGlobCompression()); |
3143 | } | 3175 | } |
3144 | #endif | 3176 | #endif |
3145 | 3177 | ||
3146 | 3178 | ||
3147 | bool PwMDoc::findSyncData(const QString &syncname, unsigned int *index) | 3179 | bool PwMDoc::findSyncData(const QString &syncname, unsigned int *index) |
3148 | { | 3180 | { |
3149 | vector<PwMSyncItem>::iterator i = dti.syncDta.begin(), | 3181 | vector<PwMSyncItem>::iterator i = dti.syncDta.begin(), |
3150 | end = dti.syncDta.end(); | 3182 | end = dti.syncDta.end(); |
3151 | 3183 | ||
3152 | while (i != end) { | 3184 | while (i != end) { |
3153 | if ((*i).syncName == syncname.latin1()) { | 3185 | if ((*i).syncName == syncname.latin1()) { |
3154 | if (index) { | 3186 | if (index) { |
3155 | *index = i - dti.syncDta.begin(); | 3187 | *index = i - dti.syncDta.begin(); |
3156 | } | 3188 | } |
3157 | return true; | 3189 | return true; |
3158 | } | 3190 | } |
3159 | ++i; | 3191 | ++i; |
3160 | } | 3192 | } |
3161 | return false; | 3193 | return false; |
3162 | }; | 3194 | }; |
3163 | 3195 | ||
3164 | /** add new syncdataentry */ | 3196 | /** add new syncdataentry */ |
3165 | PwMerror PwMDoc::addSyncDataEntry(PwMSyncItem *d, bool dontFlagDirty) | 3197 | PwMerror PwMDoc::addSyncDataEntry(PwMSyncItem *d, bool dontFlagDirty) |
3166 | { | 3198 | { |
3167 | PWM_ASSERT(d); | 3199 | PWM_ASSERT(d); |
3168 | 3200 | ||
3169 | if (isDeepLocked()) { | 3201 | if (isDeepLocked()) { |
3170 | PwMerror ret; | 3202 | PwMerror ret; |
3171 | ret = deepLock(false); | 3203 | ret = deepLock(false); |
3172 | if (ret != e_success) | 3204 | if (ret != e_success) |
3173 | return e_lock; | 3205 | return e_lock; |
3174 | } | 3206 | } |
3175 | unsigned int index; | 3207 | unsigned int index; |
3176 | 3208 | ||
3177 | const QString tmp = d->syncName.c_str(); | 3209 | const QString tmp = d->syncName.c_str(); |
3178 | bool exists = findSyncData(d->syncName.c_str(), &index); | 3210 | bool exists = findSyncData(d->syncName.c_str(), &index); |
3179 | 3211 | ||
3180 | if (exists == true) { | 3212 | if (exists == true) { |
3181 | // DOH! We found this entry. | 3213 | // DOH! We found this entry. |
3182 | return e_entryExists; | 3214 | return e_entryExists; |
3183 | } | 3215 | } |
3184 | 3216 | ||
3185 | dti.syncDta.push_back(*d); | 3217 | dti.syncDta.push_back(*d); |
3186 | 3218 | ||
3187 | if (!dontFlagDirty) | 3219 | if (!dontFlagDirty) |
3188 | flagDirty(); | 3220 | flagDirty(); |
3189 | return e_success; | 3221 | return e_success; |
3190 | } | 3222 | } |
3191 | 3223 | ||
3192 | 3224 | ||
3193 | 3225 | ||
3194 | /** delete syncdata entry */ | 3226 | /** delete syncdata entry */ |
3195 | bool PwMDoc::delSyncDataEntry(unsigned int index, bool dontFlagDirty) | 3227 | bool PwMDoc::delSyncDataEntry(unsigned int index, bool dontFlagDirty) |
3196 | { | 3228 | { |
3197 | if (isDeepLocked()) | 3229 | if (isDeepLocked()) |
3198 | return false; | 3230 | return false; |
3199 | if (index > dti.syncDta.size() - 1) | 3231 | if (index > dti.syncDta.size() - 1) |
3200 | return false; | 3232 | return false; |
3201 | 3233 | ||
3202 | // delete entry | 3234 | // delete entry |
3203 | dti.syncDta.erase(dti.syncDta.begin() + index); | 3235 | dti.syncDta.erase(dti.syncDta.begin() + index); |
3204 | 3236 | ||
3205 | if (!dontFlagDirty) | 3237 | if (!dontFlagDirty) |
3206 | flagDirty(); | 3238 | flagDirty(); |
3207 | return true; | 3239 | return true; |
3208 | } | 3240 | } |
3209 | 3241 | ||
3210 | 3242 | ||
3211 | PwMDataItem* PwMDoc::findEntryByID(const QString &uid, unsigned int *category, unsigned int *index) | 3243 | PwMDataItem* PwMDoc::findEntryByID(const QString &uid, unsigned int *category, unsigned int *index) |
3212 | { | 3244 | { |
3213 | vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(), | 3245 | vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(), |
3214 | catend = dti.dta.end(); | 3246 | catend = dti.dta.end(); |
3215 | 3247 | ||
3216 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; | 3248 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; |
3217 | 3249 | ||
3218 | while (catcounter != catend) { | 3250 | while (catcounter != catend) { |
3219 | entrBegin = catcounter->d.begin(); | 3251 | entrBegin = catcounter->d.begin(); |
3220 | entrEnd = catcounter->d.end(); | 3252 | entrEnd = catcounter->d.end(); |
3221 | entrI = entrBegin; | 3253 | entrI = entrBegin; |
3222 | while (entrI != entrEnd) { | 3254 | while (entrI != entrEnd) { |
3223 | if ((*entrI).meta.uniqueid == uid.latin1()) { | 3255 | if ((*entrI).meta.uniqueid == uid.latin1()) { |
3224 | if (category) | 3256 | if (category) |
3225 | *category = catcounter - dti.dta.begin(); | 3257 | *category = catcounter - dti.dta.begin(); |
3226 | if (index) | 3258 | if (index) |
3227 | *index = entrI - entrBegin; | 3259 | *index = entrI - entrBegin; |
3228 | 3260 | ||
3229 | return &(*entrI); | 3261 | return &(*entrI); |
3230 | } | 3262 | } |
3231 | ++entrI; | 3263 | ++entrI; |
3232 | } | 3264 | } |
3233 | ++catcounter; | 3265 | ++catcounter; |
3234 | } | 3266 | } |
3235 | 3267 | ||
3236 | return 0; | 3268 | return 0; |
3237 | } | 3269 | } |
3238 | 3270 | ||
3239 | QStringList PwMDoc::getIDEntryList() | 3271 | QStringList PwMDoc::getIDEntryList() |
3240 | { | 3272 | { |
3241 | QStringList results; | 3273 | QStringList results; |
3242 | 3274 | ||
3243 | vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(), | 3275 | vector<PwMCategoryItem>::iterator catcounter = dti.dta.begin(), |
3244 | catend = dti.dta.end(); | 3276 | catend = dti.dta.end(); |
3245 | 3277 | ||
3246 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; | 3278 | vector<PwMDataItem>::iterator entrBegin, entrEnd, entrI; |
3247 | 3279 | ||
3248 | while (catcounter != catend) { | 3280 | while (catcounter != catend) { |
3249 | entrBegin = catcounter->d.begin(); | 3281 | entrBegin = catcounter->d.begin(); |
3250 | entrEnd = catcounter->d.end(); | 3282 | entrEnd = catcounter->d.end(); |
3251 | entrI = entrBegin; | 3283 | entrI = entrBegin; |
3252 | while (entrI != entrEnd) { | 3284 | while (entrI != entrEnd) { |
3253 | results.append( (*entrI).meta.uniqueid ); | 3285 | results.append( (*entrI).meta.uniqueid ); |
3254 | ++entrI; | 3286 | ++entrI; |
3255 | } | 3287 | } |
3256 | ++catcounter; | 3288 | ++catcounter; |
3257 | } | 3289 | } |
3258 | 3290 | ||
3259 | return results; | 3291 | return results; |
3260 | } | 3292 | } |
3261 | 3293 | ||
3262 | 3294 | ||
3263 | 3295 | ||
3264 | 3296 | ||
3265 | 3297 | ||
3266 | #ifndef PWM_EMBEDDED | 3298 | #ifndef PWM_EMBEDDED |
3267 | #include "pwmdoc.moc" | 3299 | #include "pwmdoc.moc" |
3268 | #endif | 3300 | #endif |