File: duplicates.cpp

package info (click to toggle)
kpeople 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 832 kB
  • sloc: cpp: 3,723; xml: 10; makefile: 7; sh: 2
file content (119 lines) | stat: -rw-r--r-- 4,008 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
    Duplicates Example
    SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>

    SPDX-License-Identifier: LGPL-2.1-or-later
*/

#include <QCommandLineParser>
#include <QCoreApplication>
#include <QTimer>

#include <duplicatesfinder_p.h>
#include <matchessolver_p.h>
#include <personsmodel.h>

#include <cstdio>
#include <iostream>

using namespace KPeople;

class ResultPrinter : public QObject
{
    Q_OBJECT
public Q_SLOTS:
    void print(KJob *j)
    {
        QList<Match> res = ((DuplicatesFinder *)j)->results();
        std::cout << "Results: " << res.count() << std::endl;
        for (QList<Match>::iterator it = res.begin(); it != res.end();) {
            QStringList roles = it->matchReasons();
            QStringList rA;
            QStringList rB;

            AbstractContact::Ptr aA = it->indexA.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>();
            AbstractContact::Ptr aB = it->indexB.data(PersonsModel::PersonVCardRole).value<AbstractContact::Ptr>();

            Q_ASSERT(!it->reasons.isEmpty());
            for (Match::MatchReason i : std::as_const(it->reasons)) {
                rA += it->matchValue(i, aA);
                rB += it->matchValue(i, aB);
            }
            std::cout << "\t- " << qPrintable(roles.join(QStringLiteral(", "))) << ": " << it->indexA.row() << " " << it->indexB.row()
                      << " because: " << qPrintable(rA.join(QStringLiteral(", "))) << " // " << qPrintable(rB.join(QStringLiteral(", "))) << '.' << std::endl;
            bool remove = false;
            if (m_action == Ask) {
                for (char ans = ' '; ans != 'y' && ans != 'n';) {
                    std::cout << "apply? (y/n) ";
                    std::cin >> ans;
                    remove = ans == 'n';
                }
            }
            if (remove) {
                it = res.erase(it);
            } else {
                ++it;
            }
        }

        if ((m_action == Apply || m_action == Ask) && !res.isEmpty()) {
            MatchesSolver *s = new MatchesSolver(res, m_model, this);
            connect(s, SIGNAL(finished(KJob *)), this, SLOT(matchesSolverDone(KJob *)));
            s->start();
        } else {
            QCoreApplication::instance()->quit();
        }
    }

    void matchesSolverDone(KJob *job)
    {
        if (job->error() == 0) {
            std::cout << "Matching successfully finished" << std::endl;
        } else {
            std::cout << "Matching failed with error: " << job->error() << std::endl;
        }
        QCoreApplication::instance()->quit();
    }

public:
    enum MatchAction {
        Apply,
        NotApply,
        Ask,
    };
    MatchAction m_action;
    PersonsModel *m_model;
};

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    PersonsModel model;

    ResultPrinter r;
    r.m_model = &model;
    {
        QCommandLineParser parser;
        parser.addOption(QCommandLineOption(QStringLiteral("ask"), QStringLiteral("Ask whether to actually do the merge")));
        parser.addOption(QCommandLineOption(QStringLiteral("apply"), QStringLiteral("Actually apply all merges. (!!!)")));
        parser.addHelpOption();
        parser.process(app);
        r.m_action = parser.isSet(QStringLiteral("apply")) ? ResultPrinter::Apply
            : parser.isSet(QStringLiteral("ask"))          ? ResultPrinter::Ask
                                                           : ResultPrinter::NotApply;
    }

    DuplicatesFinder *f = new DuplicatesFinder(&model);
    QObject::connect(f, SIGNAL(finished(KJob *)), &r, SLOT(print(KJob *)));

    QTimer *t = new QTimer(&app);
    t->setInterval(500);
    t->setSingleShot(true);
    QObject::connect(&model, SIGNAL(modelInitialized(bool)), t, SLOT(start()));
    QObject::connect(&model, SIGNAL(rowsInserted(QModelIndex, int, int)), t, SLOT(start()));
    QObject::connect(t, SIGNAL(timeout()), f, SLOT(start()));

    app.exec();
}

#include "duplicates.moc"