summaryrefslogtreecommitdiffstats
path: root/Plugins/ConfigMigration/configmigration.cpp
blob: fe7906299783d23da7289fa8434780bf55c2f53e (plain) (blame)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "configmigration.h"
#include "services/notifymanager.h"
#include "sqlitestudio.h"
#include "mainwindow.h"
#include "statusfield.h"
#include "configmigrationwizard.h"
#include "db/dbsqlite3.h"
#include "translations.h"
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QDebug>

ConfigMigration::ConfigMigration()
{
}

bool ConfigMigration::init()
{
    SQLS_INIT_RESOURCE(configmigration);
    loadTranslation("ConfigMigration");

    if (cfg.CfgMigration.Migrated.get())
    {
        qDebug() << "ConfigMigration: already migrated. Skipping.";
        return true;
    }

    QString oldCfg = findOldConfig();
    if (!oldCfg.isNull())
    {
        db = new DbSqlite3("Old SQLiteStudio settings", oldCfg, {{DB_PURE_INIT, true}});
        if (db->open())
        {
            itemsToMigrate = findItemsToMigrate();
            notifyInfo(tr("A configuration from old SQLiteStudio 2.x.x has been detected. "
                          "Would you like to migrate old settings into the current version? "
                          "<a href=\"%1\">Click here to do that</a>.").arg(ACTION_LINK));

            connect(MAINWINDOW->getStatusField(), SIGNAL(linkActivated(QString)), this, SLOT(linkActivated(QString)));
            db->close();
        }
    }

    return true;
}

void ConfigMigration::deinit()
{
    SQLS_CLEANUP_RESOURCE(configmigration);
    safe_delete(db);

    for (ConfigMigrationItem* item : itemsToMigrate)
        delete item;

    itemsToMigrate.clear();
    GenericPlugin::deinit();
}

QString ConfigMigration::findOldConfig()
{
    QString output;
    QString dirPath;

    // Portable path 1 check
    dirPath = QDir::currentPath() + "/sqlitestudio-cfg";
    if (checkOldDir(dirPath, output))
        return output;

    // Portable path 2 check
    dirPath = QCoreApplication::applicationDirPath() + "/sqlitestudio-cfg";
    if (checkOldDir(dirPath, output))
        return output;

    // Portable path 3 check
    dirPath = QCoreApplication::applicationDirPath() + "/../sqlitestudio-cfg";
    if (checkOldDir(dirPath, output))
        return output;

    if (getDistributionType() == DistributionType::OSX_BUNDLE)
    {
        // Portable path 4 check
        dirPath = QCoreApplication::applicationDirPath() + "/../../sqlitestudio-cfg";
        if (checkOldDir(dirPath, output))
            return output;

        // Portable path 5 check
        dirPath = QCoreApplication::applicationDirPath() + "/../../../sqlitestudio-cfg";
        if (checkOldDir(dirPath, output))
            return output;
    }

    // Global path check
#ifdef Q_OS_WIN
    if (QSysInfo::windowsVersion() & QSysInfo::WV_NT_based)
        dirPath = SQLITESTUDIO->getEnv("APPDATA")+"/sqlitestudio";
    else
        dirPath = SQLITESTUDIO->getEnv("HOME")+"/sqlitestudio";
#else
    dirPath = SQLITESTUDIO->getEnv("HOME")+"/.sqlitestudio";
#endif

    if (checkOldDir(dirPath, output))
        return output;

    return QString();
}

bool ConfigMigration::checkOldDir(const QString &dir, QString &output)
{
    QFileInfo fi(dir + "/settings");
    if (fi.exists() && fi.isReadable())
    {
        output = fi.absoluteFilePath();
        return true;
    }

    return false;
}

QList<ConfigMigrationItem*> ConfigMigration::findItemsToMigrate()
{
    static_qstring(bugsHistoryQuery, "SELECT count(*) FROM bugs");
    static_qstring(dbListQuery, "SELECT count(*) FROM dblist");
    static_qstring(funcListQuery, "SELECT count(*) FROM functions");
    static_qstring(sqlHistoryQuery, "SELECT count(*) FROM history");

    ConfigMigrationItem* item = nullptr;
    QList<ConfigMigrationItem*> results;

    int bugReports = db->exec(bugsHistoryQuery)->getSingleCell().toInt();
    if (bugReports > 0)
    {
        item = new ConfigMigrationItem;
        item->type = ConfigMigrationItem::Type::BUG_REPORTS;
        item->label = tr("Bug reports history (%1)").arg(bugReports);
        results << item;
    }

    int dbCount = db->exec(dbListQuery)->getSingleCell().toInt();
    if (dbCount > 0)
    {
        item = new ConfigMigrationItem;
        item->type = ConfigMigrationItem::Type::DATABASES;
        item->label = tr("Database list (%1)").arg(dbCount);
        results << item;
    }

    int funcCount = db->exec(funcListQuery)->getSingleCell().toInt();
    if (funcCount > 0)
    {
        item = new ConfigMigrationItem;
        item->type = ConfigMigrationItem::Type::FUNCTION_LIST;
        item->label = tr("Custom SQL functions (%1)").arg(funcCount);
        results << item;
    }

    int sqlHistory = db->exec(sqlHistoryQuery)->getSingleCell().toInt();
    if (sqlHistory > 0)
    {
        item = new ConfigMigrationItem;
        item->type = ConfigMigrationItem::Type::SQL_HISTORY;
        item->label = tr("SQL queries history (%1)").arg(sqlHistory);
        results << item;
    }

    return results;
}

Db* ConfigMigration::getOldCfgDb() const
{
    return db;
}

QList<ConfigMigrationItem*> ConfigMigration::getItemsToMigrate() const
{
    return itemsToMigrate;
}

void ConfigMigration::linkActivated(const QString &link)
{
    if (link != ACTION_LINK)
        return;

    ConfigMigrationWizard wizard(MAINWINDOW, this);
    wizard.exec();

    if (wizard.didMigrate())
        cfg.CfgMigration.Migrated.set(true);
}