~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to plugins/history/historyimport.cpp

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
157
157
                amount++; // for QProgressDialog in save()
158
158
 
159
159
                strings[0] = log->other->protocol()->pluginId() + " (" + log->other->account()->accountId() + ')';
160
 
                strings[1] = log->other->nickName();
 
160
                strings[1] = log->other->displayName();
161
161
                strings[2] = message.timestamp.toString("yyyy-MM-dd");
162
162
 
163
163
                bool update = false;
213
213
                                continue;
214
214
                        cursor.insertText(message.timestamp.toString("hh:mm:ss "));
215
215
                        if (message.incoming)
216
 
                                cursor.insertText(log.other->nickName().append(": "));
 
216
                                cursor.insertText(log.other->displayName().append(": "));
217
217
                        else
218
 
                                cursor.insertText(log.me->nickName().append(": "));
 
218
                                cursor.insertText(log.me->displayName().append(": "));
219
219
                        cursor.insertText(message.text);
220
220
                        cursor.insertBlock();
221
221
                }
275
275
        progress.show();
276
276
        cancel = false;
277
277
 
 
278
        // protocolMap maps pidgin account-names to kopete protocol names (as in Kopete::Contact::protocol()->pluginId())
 
279
        QHash<QString, QString> protocolMap;
 
280
        protocolMap.insert("msn", "WlmProtocol");
 
281
        protocolMap.insert("icq", "ICQProtocol");
 
282
        protocolMap.insert("aim", "AIMProtocol");
 
283
        protocolMap.insert("jabber", "JabberProtocol");
 
284
        protocolMap.insert("yahoo", "YahooProtocol");
 
285
        protocolMap.insert("qq", "QQProtocol");
 
286
        protocolMap.insert("irc", "IRCProtocol");
 
287
        protocolMap.insert("gadu-gadu", "GaduProtocol");
 
288
        protocolMap.insert("bonjour", "BonjourProtocol");
 
289
        protocolMap.insert("meanwhile", "MeanwhileProtocol");
 
290
 
278
291
        QString protocolFolder;
279
292
        foreach (protocolFolder, logDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
280
293
                logDir.cd(protocolFolder);
283
296
                foreach (accountFolder, logDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
284
297
                        logDir.cd(accountFolder);
285
298
 
 
299
                        // check if we can map the protocol
 
300
                        if (!protocolMap.contains(protocolFolder)) {
 
301
                                detailsCursor.insertText(i18n("WARNING: There is no equivalent for protocol %1 in kopete.\n", protocolFolder));
 
302
                                logDir.cdUp();
 
303
                                continue;
 
304
                        }
 
305
                        const QString & protocol = protocolMap.value(protocolFolder);
 
306
 
286
307
                        // TODO use findContact?
287
308
                        Kopete::ContactList * cList = Kopete::ContactList::self();
288
309
                        QList<Kopete::Contact *> meList = cList->myself()->contacts();
289
310
                        Kopete::Contact *me;
290
311
                        bool found = false;
291
312
                        foreach (me, meList) {
292
 
                                if (me->protocol()->pluginId().contains(protocolFolder, Qt::CaseInsensitive) &&
293
 
                                 me->account()->accountId().contains(accountFolder, Qt::CaseInsensitive)) {
 
313
                                if (me->protocol()->pluginId() == protocol && me->account()->accountId().contains(accountFolder, Qt::CaseInsensitive)) {
294
314
                                        found = true;
295
315
                                        break;
296
316
                                }
360
380
 
361
381
}
362
382
 
363
 
bool HistoryImport::isNickIncoming(const QString &nick, struct Log *log)
364
 
{
365
 
        bool incoming;
366
 
 
367
 
        if (nick == log->me->nickName())
368
 
                incoming = false;
369
 
        else if (nick == log->other->nickName())
370
 
                incoming = true;
371
 
        else if (knownNicks.contains(nick)) 
372
 
                incoming = knownNicks.value(nick);
373
 
        else {
374
 
                int r = QMessageBox::question(NULL,
375
 
                        i18n("Cannot Map Nickname to Account"),
376
 
                        i18n("Did you use \"%1\" as nickname in history?", nick),
377
 
                        QMessageBox::Yes | QMessageBox::No | QMessageBox::Abort);
378
 
 
379
 
                if (r == QMessageBox::Yes) {
380
 
                        knownNicks.insert(nick, true);
381
 
                        incoming = true;
382
 
                }
383
 
                else if (r == QMessageBox::No) {
384
 
                        knownNicks.insert(nick, false);
385
 
                        incoming = false;
386
 
                }
387
 
                else {
388
 
                        cancel = true;
389
 
                        return false;
390
 
                }
391
 
        }
392
 
        return incoming;
393
 
}
394
 
 
395
383
QDateTime HistoryImport::extractTime(const QString &string, QDate ref)
396
384
{
397
385
        QDateTime dateTime;
428
416
 
429
417
void HistoryImport::parsePidginTxt(QFile &file, struct Log *log, QDate date)
430
418
{
431
 
        QByteArray line;
432
 
        QTime time;
433
 
        QDateTime dateTime;
434
 
        QString messageText, nick;
435
 
        bool incoming = false; // =false to make the compiler not complain
436
 
 
437
 
        while (!file.atEnd()) {
438
 
                line = file.readLine();
 
419
        QString line;
 
420
        QString nick;
 
421
        struct Message message;
 
422
 
 
423
        // this is to collect unknown nicknames (the list stores the index in log->messages of the messages that used the nickname)
 
424
        // the bool says if that nickname is incoming (only used when the list is empty)
 
425
        typedef QHash<QString, QPair<bool, QList<int> > > NickNameHash;
 
426
        NickNameHash nicknames;
 
427
 
 
428
        QTextStream str(&file);
 
429
        // utf-8 seems to be default for pidgins-txt logs
 
430
        str.setCodec("UTF-8");
 
431
 
 
432
        while (!str.atEnd()) {
 
433
                line = str.readLine();
439
434
 
440
435
                if (line[0] == '(') {
441
 
                        if (!messageText.isEmpty()) {
442
 
                                // messageText contains an unwished newline at the end
443
 
                                if (messageText.endsWith('\n'))
444
 
                                        messageText.remove(-1, 1);
445
 
                                struct Message message;
446
 
                                message.incoming = incoming;
447
 
                                message.text = messageText;
448
 
                                message.timestamp = dateTime;
 
436
                        if (!message.text.isEmpty()) {
 
437
                                /*// message.text contains an unwished newline at the end
 
438
                                if (message.text.endsWith('\n'))
 
439
                                        message.text.chop(1); */
449
440
                                log->messages.append(message);
450
 
                                messageText.clear();
 
441
                                message.text.clear();
451
442
                        }
452
443
 
453
444
                        int endTime = line.indexOf(')')+1;
454
 
                        dateTime = extractTime(line.left(endTime), date);
 
445
                        message.timestamp = extractTime(line.left(endTime), date);
455
446
 
456
447
                        int nickEnd = QRegExp("\\s").indexIn(line, endTime + 1);
457
448
                        // TODO what if a nickname consists of two words? is this possible?
463
454
 
464
455
                        nick = line.mid(endTime+1, nickEnd - endTime - 2); // -2 to delete the colon
465
456
 
466
 
                        incoming = isNickIncoming(nick, log);
 
457
                        // detect if the message is in- or outbound
 
458
                        if (nick == log->me->displayName())
 
459
                                message.incoming = false;
 
460
                        else if (nick == log->other->displayName())
 
461
                                message.incoming = true;
 
462
                        else if (knownNicks.contains(nick))
 
463
                                message.incoming = knownNicks.value(nick);
 
464
                        else {
 
465
                                // store this nick for later decision
 
466
                                nicknames[nick].second.append(log->messages.size());
 
467
                        }
 
468
                        nicknames[nick].first = message.incoming;
 
469
 
467
470
                        if (cancel)
468
471
                                return;
469
472
 
470
 
                        messageText = line.mid(nickEnd + 1);
 
473
                        message.text = line.mid(nickEnd + 1);
471
474
                }
472
475
                else if (line[0] == ' ') {
473
476
                        // an already started message is continued in this line
474
477
                        int start = QRegExp("\\S").indexIn(line);
475
 
                        messageText.append('\n' + line.mid(start));
 
478
                        message.text.append('\n' + line.mid(start));
476
479
                }
477
480
        }
478
 
        if (!messageText.isEmpty()) {
479
 
                struct Message message;
480
 
                message.incoming = incoming;
481
 
                message.text = messageText;
482
 
                message.timestamp = dateTime;
 
481
        if (!message.text.isEmpty())
483
482
                log->messages.append(message);
484
 
                messageText.clear();
 
483
 
 
484
        // check if we can guess which nickname belongs to us
 
485
        NickNameHash::iterator itr;
 
486
        NickNameHash::const_iterator itr2;
 
487
        for (itr = nicknames.begin(); itr != nicknames.end(); ++itr) {
 
488
                if (itr->second.isEmpty()) // no work for this one
 
489
                        continue;
 
490
                bool haveAnother = false, lastIncoming = false;
 
491
                // check against all other nicknames
 
492
                for (itr2 = nicknames.constBegin(); itr2 != nicknames.constEnd(); ++itr2) {
 
493
                        if (itr2 == NickNameHash::const_iterator(itr)) // skip ourselve
 
494
                                continue;
 
495
 
 
496
                        // if there is another unknown nickname, we have no chance to guess which is our
 
497
                        if (!itr2->second.isEmpty())
 
498
                                break;
 
499
                        if (!haveAnother) {
 
500
                                lastIncoming = itr2->first;
 
501
                                haveAnother = true;
 
502
                        } else {
 
503
                                // when there are more than one known nicknames, but with different incoming-values, we also can't guess which is ours
 
504
                                if (lastIncoming != itr2->first)
 
505
                                        break;
 
506
                        }
 
507
                }
 
508
                // we now can guess the incoming value of itr, namely !lastIncoming
 
509
                if (haveAnother && itr2 == nicknames.constEnd()) {
 
510
                        // inform the user
 
511
                        if (lastIncoming)
 
512
                                detailsCursor.insertText(i18n("INFORMATION: Guessed %1 to be one of your nicks.\n", itr.key()));
 
513
                        else
 
514
                                detailsCursor.insertText(i18n("INFORMATION: Guessed %1 to be one of your buddys nicks.\n", itr.key()));
 
515
 
 
516
                        knownNicks.insert(itr.key(), !lastIncoming);
 
517
                        int i;
 
518
                        for (i = 0; i < itr->second.size(); i++)
 
519
                                log->messages[itr->second.at(i)].incoming = !lastIncoming;
 
520
                        itr->second.clear(); // we are finished with theese indexes
 
521
                }
 
522
        }
 
523
 
 
524
        // iterate once again over the nicknames to detect which nicks are still not known. simply ask the user!
 
525
        for (itr = nicknames.begin(); itr != nicknames.end(); ++itr) {
 
526
                if (itr->second.isEmpty()) // no word for this one
 
527
                        continue;
 
528
 
 
529
                bool incoming;
 
530
                int r = QMessageBox::question(NULL,
 
531
                        i18n("Cannot map Nickname to Account"),
 
532
                        i18n("Did you ever use \"%1\" as nickname in your history?", itr.key()),
 
533
                        QMessageBox::Yes | QMessageBox::No | QMessageBox::Abort);
 
534
 
 
535
                if (r == QMessageBox::Yes) {
 
536
                        knownNicks.insert(itr.key(), false);
 
537
                        incoming = true;
 
538
                }
 
539
                else if (r == QMessageBox::No) {
 
540
                        knownNicks.insert(itr.key(), true);
 
541
                        incoming = false;
 
542
                }
 
543
                else {
 
544
                        cancel = true;
 
545
                        return;
 
546
                }
 
547
 
 
548
                // set the queried incoming value to our already stored Messages
 
549
                int i;
 
550
                for (i = 0; i < itr->second.size(); i++)
 
551
                        log->messages[itr->second.at(i)].incoming = incoming;
485
552
        }
486
553
}
487
554