Subversion Repositories harbour-webkitNG

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 devnull 1
#include "myclass.h"
2
//#include <QNetworkCookieJar>
3
//#include <QNetworkAccessManager>
4
#include <QDebug>
5
 
6
 
7
MyClass::MyClass(QQuickView *v)
8
{
9
//    view = v;
10
//    engine = new QQmlEngine();
11
//    engine = view->engine();
12
    h = myHome->homePath();
13
 
14
    config_dir = QDir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation))
15
            .filePath(QCoreApplication::applicationName());
16
 
17
    data_dir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
18
 
19
    cache_dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
20
 
21
    documents_dir = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
22
 
23
 
24
    // Make sure mimer is able to set webcat as default browser
25
    QFile cpFile;
26
    if (!isFile(h + "/.local/share/applications/harbour-webcat-open-url.desktop")) {
27
        cpFile.copy("/usr/share/harbour-webcat/harbour-webcat-open-url.desktop", h + "/.local/share/applications/harbour-webcat-open-url.desktop");
28
    }
29
    if (!isFile(h + "/.local/share/applications/open-url.desktop")) {
30
        cpFile.copy("/usr/share/applications/open-url.desktop", h + "/.local/share/applications/open-url.desktop");
31
        QProcess *proc = new QProcess();
32
        proc->start("update-desktop-database "+ h +"/.local/share/applications");
33
    }
34
    if (!existsPath(h + "/.local/share/dbus-1/services")) {
35
        QDir makePath;
36
        makePath.mkpath(h + "/.local/share/dbus-1/services");
37
    }
38
    if (!isFile(h + "/.local/share/dbus-1/services/org.harbour.webcat.service")) {
39
        cpFile.copy("/usr/share/harbour-webcat/org.harbour.webcat.service", h+ "/.local/share/dbus-1/services/org.harbour.webcat.service");
40
    }
41
 
42
    //qDebug() << data_dir;
43
}
44
 
45
MyClass::~MyClass() {
46
    if (data_dir.contains("PRIVATE")) {
47
        qDebug() << "[myclass.cpp]: Seems to be a private browsing mode so remove the data_dir for it...";
48
        clear(data_dir); // Site specific stuff
49
        clear(data_dir + "/.QtWebKit"); // The rest (cookies, bookmarks, settings and so on)
50
    }
51
}
52
 
53
// Not used for now until I figure out how to implement this in a sane manner.
54
// TODO: Instead of this I should somehow grab the NetworkAccessManager of WebView but I don't know how it is implemented in Qt5
55
//       as settings view->engine()->setNetworkAccessManager()
56
void MyClass::privateMode() {
57
    QFile *myFolder = new QFile();
58
    myFolder->setFileName(h + "/.local/share/harbour-webcat/.QtWebKit");
59
    myFolder->rename(".QtWebkit", ".QtWebkit-Private");
60
    // Start webcat in Private Mode
61
    QProcess *proc = new QProcess();
62
    proc->startDetached("/usr/bin/harbour-webcat");
63
}
64
 
65
//void MyClass::clearCookies() {
66
//    //privateMode();
67
//    QFile *cookieF = new QFile();
68
//    //qDebug() << data_dir + "/.QtWebKit/cookies.db";
69
//    cookieF->setFileName(data_dir + "/.QtWebKit/cookies.db");
70
//    cookieF->remove();
71
 
72
//    // Clear LocalStorage
73
//    clear(data_dir + "/.QtWebKit/LocalStorage");
74
 
75
//    // Reload webcat
76
//    QProcess *proc = new QProcess();
77
//    proc->startDetached("/usr/bin/harbour-webcat");
78
//    // Efficient way to crash the app which is intended here :P
79
//    view->close();
80
//}
81
 
82
void MyClass::clear(QString dir) {
83
    QDir real_dir(dir);
84
 
85
    qDebug() << real_dir;
86
 
87
    //First delete any files in the current directory
88
    QFileInfoList files = real_dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files);
89
    qDebug() << "Files : " + files.count();
90
    for(int file = 0; file < files.count(); file++)
91
    {
92
        qDebug() << "Removing file " + QString(files.at(file).fileName());
93
        real_dir.remove(files.at(file).fileName());
94
    }
95
 
96
    //Now recursively delete any child directories
97
    QFileInfoList dirs = real_dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Dirs);
98
    qDebug() << "Dirs : " + dirs.count();
99
    for(int dir = 0; dir < dirs.count(); dir++)
100
    {
101
        qDebug() << "Start removing files in " + QString(dirs.at(dir).absoluteFilePath());
102
        this->clear(dirs.at(dir).absoluteFilePath());
103
    }
104
}
105
 
106
void MyClass::clearCache() {
107
    // Clear DiskCache
108
    clear(cache_dir + "/.QtWebKit/DiskCache");
109
}
110
 
111
void MyClass::openNewWindow(const QString &url) {
112
    QProcess *proc = new QProcess();
113
    proc->startDetached("/usr/bin/harbour-webcat --new-window " + url);
114
}
115
 
116
void MyClass::openPrivateNewWindow(const QString &url) {
117
    QProcess *proc = new QProcess();
118
    proc->startDetached("/usr/bin/harbour-webcat --private " + url);
119
}
120
 
121
void MyClass::openWithvPlayer(const QString &url) {
122
    qDebug() << "[myclass.cpp]: trying to start harbour-videoPlayer -p " + url;
123
    QProcess *proc = new QProcess();
124
    proc->startDetached("/usr/bin/harbour-videoPlayer -p \"" + url + "\"");
125
}
126
 
127
void MyClass::openExternally(const QString &url) {
128
    qDebug() << "[myclass.cpp]: trying to start " + url;
129
    QProcess *proc = new QProcess();
130
    proc->startDetached("xdg-open \"" + url + "\"");
131
}
132
 
133
void MyClass::runExternally(const QString &cmd) {
134
    qDebug() << "[myclass.cpp]: trying to start " + cmd;
135
    QProcess *proc = new QProcess();
136
    proc->startDetached(cmd);
137
}
138
 
139
bool MyClass::isFile(const QString &url)
140
{
141
    return QFileInfo(url).isFile();
142
}
143
 
144
bool MyClass::existsPath(const QString &url)
145
{
146
    return QDir(url).exists();
147
}
148
 
149
void MyClass::setMime(const QString &mimeType, const QString &desktopFile)
150
{
151
    QProcess mimeProc;
152
    mimeProc.start("xdg-mime default " + desktopFile + " " + mimeType);
153
    mimeProc.waitForFinished();
154
    // Workaround for SailfishOS which only works if defaults.list is available. Xdg-mime only produces mimeapps.list however
155
    if (!isFile(h + "/.local/share/applications/defaults.list"))  {
156
        if (isFile(h + "/.local/share/applications/mimeapps.list")) {
157
            QProcess linking;
158
            linking.start("ln -sf " + h + "/.config/mimeapps.list " + h + "/.local/share/applications/defaults.list");
159
            linking.waitForFinished();
160
        }
161
        // Newer SailfishOS Versions put mimeapps.list in config
162
        if (isFile(h + "/.config/mimeapps.list")) {
163
            QProcess linking;
164
            linking.start("ln -sf " + h + "/.config/mimeapps.list " + h + "/.local/share/applications/defaults.list");
165
            linking.waitForFinished();
166
        }
167
    }
168
}
169
 
170
void MyClass::setDefaultBrowser()
171
{
172
    setMime("text/html", "harbour-webcat-open-url.desktop");
173
    setMime("x-scheme-handler/http", "harbour-webcat-open-url.desktop");
174
    setMime("x-scheme-handler/https", "harbour-webcat-open-url.desktop");
175
}
176
 
177
void MyClass::resetDefaultBrowser()
178
{
179
    setMime("text/html", "open-url.desktop");
180
    setMime("x-scheme-handler/http", "open-url.desktop");
181
    setMime("x-scheme-handler/https", "open-url.desktop");
182
}
183
 
184
void MyClass::backupConfig()
185
{
186
    backupConfig("webcat_backup" + curDate.currentDateTime().toString("yyyy_MM_dd_hh_mm_ss") +".tar.gz");
187
}
188
 
189
void MyClass::backupConfig(QString backupName)
190
{
191
    if (existsPath(data_dir)) {
192
        if (backupName.isEmpty())
193
            backupName = "webcat_backup" + curDate.currentDateTime().toString("yyyy_MM_dd_hh_mm_ss") +".tar.gz";
194
        compress.start("tar -zcf " + documents_dir + "/" + backupName + " " + data_dir + "/");
195
        connect(&compress, SIGNAL(finished(int)), this, SLOT(getCompressStatus(int)));
196
    }
197
    else {
198
        errorMsg = tr("Webcat config dir not found"); // This should never happen
199
        error(errorMsg);
200
    }
201
}
202
 
203
void MyClass::getCompressStatus(int exitCode)
204
{
205
    if (exitCode == 0) {
206
        backupComplete();
207
    }
208
    else {
209
        QByteArray errorOut = compress.readAllStandardError();
210
        qDebug() << "Called the C++ slot and got following error:" << errorOut.simplified();
211
        errorMsg = errorOut.simplified();
212
        error(errorMsg);
213
    }
214
}
215
 
216
void MyClass::checkBackup(QString backupFile)
217
{
218
    //qDebug() << "[myclass.cpp] Called with backupFile:" + backupFile;
219
    if (isFile(backupFile)) {
220
        curBackupFile = backupFile;
221
        checkProcess.start("bash", QStringList() << "-c" << "tar -tf \"" + backupFile + "\" | grep harbour-webcat -cim1");
222
        connect(&checkProcess, SIGNAL(finished(int)), this, SLOT(getCheckStatus(int)));
223
    }
224
    else {
225
        curBackupFile = "";
226
        qDebug() << "[myclass.cpp] backupFile does not exist";
227
        errorMsg = tr("File not found.");
228
        error(errorMsg);
229
    }
230
}
231
 
232
void MyClass::getCheckStatus(int exitCode)
233
{
234
    if (exitCode == 0){
235
        QByteArray checkoutput = checkProcess.readAllStandardOutput();
236
        qDebug() << "Got following checkProcess output:" << checkoutput.simplified();
237
        if (checkoutput.simplified() == "1") {
238
            validBackupFile = true;
239
            // extract Backup
240
            restoreBackup();
241
        } else {
242
            validBackupFile = false;
243
            errorMsg = tr("No valid Backup file. Did not find harbour-webcat Folder.");
244
            error(errorMsg);
245
        }
246
    } else {
247
        QByteArray checkerror = checkProcess.readAllStandardError();
248
        qDebug() << "[myclass.cpp] Got following checkProcess error:" << checkerror.simplified();
249
        validBackupFile = false;
250
        errorMsg = tr("Could not verify Backup file.\n") + checkerror.simplified();
251
        error(errorMsg);
252
    }
253
}
254
 
255
void MyClass::restoreBackup()
256
{
257
    if (validBackupFile) {
258
        // TODO: Using -C / might be dangerous here as it might write other files aswell to users home directory
259
        //       if backup file is manipulated. Evaluate if extracting to /tmp and only copying over harbour-webcat folder
260
        //       makes more sense.
261
        decompress.start("tar -xzf " + curBackupFile + " -C /");
262
        connect(&decompress, SIGNAL(finished(int)), this, SLOT(getDecompressStatus(int)));
263
    } else {
264
        errorMsg = tr("No valid Backup file. Did not find harbour-webcat Folder.");
265
        error(errorMsg);
266
    }
267
}
268
 
269
void MyClass::getDecompressStatus(int exitCode)
270
{
271
    if (exitCode == 0) {
272
        restoreComplete();
273
    }
274
    else {
275
        QByteArray errorOut = decompress.readAllStandardError();
276
        qDebug() << "Called the C++ slot and got following error:" << errorOut.simplified();
277
        errorMsg = errorOut.simplified();
278
        error(errorMsg);
279
    }
280
}
281
 
282
void MyClass::copy2clipboard(QString text)
283
{
284
    if (text.isEmpty()) {
285
        return;
286
    }
287
    else {
288
        QClipboard *cb;
289
        cb->clear();
290
        cb->setText(text);
291
    }
292
}
293
void MyClass::remove(const QString &url)
294
{    //qDebug() << "Called the C++ slot and request removal of:" << url;
295
    QFile(url).remove();
296
}
297
 
298
void MyClass::createDesktopLauncher(QString favIcon, QString title, QString url)
299
{
300
    if (favIcon != "") {
301
        // TODO Download favIcon
302
    }
303
    else favIcon = "icon-launcher-browser";
304
    if (url == "") return;
305
    else {
306
        QUrl launcherUrl(url);
307
        QString host = launcherUrl.host();
308
        QString fname = launcherUrl.fileName();
309
        if (title == "") title = host + "-" + fname;
310
        QString launcherPath = h + "/.local/share/applications/"+ host + "-" + fname + ".desktop";
311
        /* Search if file exists. If yes remove it */
312
        if (isFile(launcherPath)) remove(launcherPath);
313
 
314
        /* Try and open a file for output */
315
        QString outputFilename = launcherPath;
316
        QFile outputFile(outputFilename);
317
        outputFile.open(QIODevice::WriteOnly);
318
 
319
        /* Check it opened OK */
320
        if(!outputFile.isOpen()){
321
            qDebug() << "Error, unable to open" << outputFilename << "for output";
322
            return;
323
        }
324
 
325
        /* Point a QTextStream object at the file */
326
        QTextStream outStream(&outputFile);
327
 
328
        /* Write the line to the file */
329
        outStream << "[Desktop Entry]\n";
330
        outStream << "Type=Application\n";
331
        outStream << "Name=" + title + "\n";
332
        outStream << "Icon=" + favIcon + "\n";
333
        outStream << "Exec=harbour-webcat '" + url + "'\n";
334
 
335
        /* Close the file */
336
        outputFile.close();
337
        return;
338
    }
339
}