2014-03-09 21:51:42 +01:00
|
|
|
/* ============================================================
|
2017-08-25 17:55:52 +02:00
|
|
|
* Falkon - Qt web browser
|
2018-01-22 14:15:14 +01:00
|
|
|
* Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
|
2014-03-09 21:51:42 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "profilemanager.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "datapaths.h"
|
|
|
|
#include "updater.h"
|
|
|
|
#include "qztools.h"
|
2018-01-22 14:15:14 +01:00
|
|
|
#include "restoremanager.h"
|
2018-01-28 12:49:35 +01:00
|
|
|
#include "sqldatabase.h"
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
#include <QDir>
|
2017-11-13 13:25:30 +01:00
|
|
|
#include <QSqlError>
|
2017-12-18 13:06:42 +01:00
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QSqlDatabase>
|
2014-03-09 21:51:42 +01:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
ProfileManager::ProfileManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-28 20:44:15 +02:00
|
|
|
void ProfileManager::initConfigDir()
|
2014-03-09 21:51:42 +01:00
|
|
|
{
|
|
|
|
QDir dir(DataPaths::path(DataPaths::Config));
|
|
|
|
|
|
|
|
if (dir.exists() && QFile(dir.filePath(QLatin1String("profiles/profiles.ini"))).exists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-25 17:11:29 +02:00
|
|
|
std::cout << "Falkon: Creating new profile directory" << std::endl;
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
dir.mkpath(dir.absolutePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.mkdir(QLatin1String("profiles"));
|
|
|
|
dir.cd(QLatin1String("profiles"));
|
|
|
|
|
|
|
|
// $Config/profiles
|
2015-08-18 10:13:44 +02:00
|
|
|
QFile(dir.filePath(QLatin1String("profiles.ini"))).remove();
|
|
|
|
QFile(QLatin1String(":data/profiles.ini")).copy(dir.filePath(QLatin1String("profiles.ini")));
|
|
|
|
QFile(dir.filePath(QLatin1String("profiles.ini"))).setPermissions(QFile::ReadUser | QFile::WriteUser);
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
dir.mkdir(QLatin1String("default"));
|
|
|
|
dir.cd(QLatin1String("default"));
|
|
|
|
|
|
|
|
// $Config/profiles/default
|
2015-08-18 10:13:44 +02:00
|
|
|
QFile(QLatin1String(":data/bookmarks.json")).copy(dir.filePath(QLatin1String("bookmarks.json")));
|
|
|
|
QFile(dir.filePath(QLatin1String("bookmarks.json"))).setPermissions(QFile::ReadUser | QFile::WriteUser);
|
2014-03-09 21:51:42 +01:00
|
|
|
|
2015-08-18 10:13:44 +02:00
|
|
|
QFile versionFile(dir.filePath(QLatin1String("version")));
|
2014-03-09 21:51:42 +01:00
|
|
|
versionFile.open(QFile::WriteOnly);
|
2014-04-17 15:20:54 +02:00
|
|
|
versionFile.write(Qz::VERSION);
|
2014-03-09 21:51:42 +01:00
|
|
|
versionFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileManager::initCurrentProfile(const QString &profileName)
|
|
|
|
{
|
2014-03-09 22:17:13 +01:00
|
|
|
QString profilePath = DataPaths::path(DataPaths::Profiles) + QLatin1Char('/');
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
if (profileName.isEmpty()) {
|
|
|
|
profilePath.append(startingProfile());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
profilePath.append(profileName);
|
|
|
|
}
|
|
|
|
|
2014-03-09 22:17:13 +01:00
|
|
|
DataPaths::setCurrentProfilePath(profilePath);
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
updateCurrentProfile();
|
|
|
|
connectDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ProfileManager::createProfile(const QString &profileName)
|
|
|
|
{
|
|
|
|
QDir dir(DataPaths::path(DataPaths::Profiles));
|
|
|
|
|
|
|
|
if (QDir(dir.absolutePath() + QLatin1Char('/') + profileName).exists()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!dir.mkdir(profileName)) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.cd(profileName);
|
|
|
|
|
2015-08-18 10:13:44 +02:00
|
|
|
QFile versionFile(dir.filePath(QLatin1String("version")));
|
2014-03-09 21:51:42 +01:00
|
|
|
versionFile.open(QFile::WriteOnly);
|
2014-04-17 15:20:54 +02:00
|
|
|
versionFile.write(Qz::VERSION);
|
2014-03-09 21:51:42 +01:00
|
|
|
versionFile.close();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProfileManager::removeProfile(const QString &profileName)
|
|
|
|
{
|
2014-12-30 14:51:36 +01:00
|
|
|
QDir dir(DataPaths::path(DataPaths::Profiles) + QLatin1Char('/') + profileName);
|
2014-03-09 21:51:42 +01:00
|
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QzTools::removeDir(dir.absolutePath());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-28 20:39:10 +02:00
|
|
|
// static
|
|
|
|
QString ProfileManager::currentProfile()
|
2014-03-09 21:51:42 +01:00
|
|
|
{
|
|
|
|
QString path = DataPaths::currentProfilePath();
|
|
|
|
return path.mid(path.lastIndexOf(QLatin1Char('/')) + 1);
|
|
|
|
}
|
|
|
|
|
2015-09-28 20:39:10 +02:00
|
|
|
// static
|
|
|
|
QString ProfileManager::startingProfile()
|
2014-03-09 21:51:42 +01:00
|
|
|
{
|
2014-03-09 22:17:13 +01:00
|
|
|
QSettings settings(DataPaths::path(DataPaths::Profiles) + QLatin1String("/profiles.ini"), QSettings::IniFormat);
|
2014-03-09 21:51:42 +01:00
|
|
|
return settings.value(QLatin1String("Profiles/startProfile"), QLatin1String("default")).toString();
|
|
|
|
}
|
|
|
|
|
2015-09-28 20:39:10 +02:00
|
|
|
// static
|
2014-03-09 21:51:42 +01:00
|
|
|
void ProfileManager::setStartingProfile(const QString &profileName)
|
|
|
|
{
|
2014-03-09 22:17:13 +01:00
|
|
|
QSettings settings(DataPaths::path(DataPaths::Profiles) + QLatin1String("/profiles.ini"), QSettings::IniFormat);
|
2014-03-09 21:51:42 +01:00
|
|
|
settings.setValue(QLatin1String("Profiles/startProfile"), profileName);
|
|
|
|
}
|
|
|
|
|
2015-09-28 20:39:10 +02:00
|
|
|
// static
|
|
|
|
QStringList ProfileManager::availableProfiles()
|
2014-03-09 21:51:42 +01:00
|
|
|
{
|
|
|
|
QDir dir(DataPaths::path(DataPaths::Profiles));
|
|
|
|
return dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileManager::updateCurrentProfile()
|
|
|
|
{
|
|
|
|
QDir profileDir(DataPaths::currentProfilePath());
|
|
|
|
|
|
|
|
if (!profileDir.exists()) {
|
|
|
|
QDir newDir(profileDir.path().remove(profileDir.dirName()));
|
|
|
|
newDir.mkdir(profileDir.dirName());
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile versionFile(profileDir.filePath(QLatin1String("version")));
|
|
|
|
|
|
|
|
// If file exists, just update the profile to current version
|
|
|
|
if (versionFile.exists()) {
|
|
|
|
versionFile.open(QFile::ReadOnly);
|
|
|
|
QString profileVersion = versionFile.readAll();
|
|
|
|
versionFile.close();
|
|
|
|
|
|
|
|
updateProfile(Qz::VERSION, profileVersion.trimmed());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
copyDataToProfile();
|
|
|
|
}
|
|
|
|
|
|
|
|
versionFile.open(QFile::WriteOnly);
|
2014-04-17 15:20:54 +02:00
|
|
|
versionFile.write(Qz::VERSION);
|
2014-03-09 21:51:42 +01:00
|
|
|
versionFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileManager::updateProfile(const QString ¤t, const QString &profile)
|
|
|
|
{
|
|
|
|
if (current == profile) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Updater::Version prof(profile);
|
|
|
|
|
2018-01-22 14:15:14 +01:00
|
|
|
if (prof > Updater::Version(Qz::VERSION)) {
|
|
|
|
copyDataToProfile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:52:40 +02:00
|
|
|
if (prof < Updater::Version("1.9.0")) {
|
2017-08-25 17:11:29 +02:00
|
|
|
std::cout << "Falkon: Using profile from QupZilla " << qPrintable(profile) << " is not supported!" << std::endl;
|
2014-10-11 13:11:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-08 16:24:44 +02:00
|
|
|
// No change in 2.0
|
|
|
|
if (prof < Updater::Version("2.0.99")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-04 10:46:48 +01:00
|
|
|
// No change in 2.1
|
|
|
|
if (prof < Updater::Version("2.1.99")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:52:40 +02:00
|
|
|
// Nothing for now
|
2014-03-09 21:51:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileManager::copyDataToProfile()
|
|
|
|
{
|
|
|
|
QDir profileDir(DataPaths::currentProfilePath());
|
|
|
|
|
|
|
|
QFile browseData(profileDir.filePath(QLatin1String("browsedata.db")));
|
|
|
|
|
|
|
|
if (browseData.exists()) {
|
|
|
|
const QString browseDataBackup = QzTools::ensureUniqueFilename(profileDir.filePath(QLatin1String("browsedata-backup.db")));
|
|
|
|
browseData.copy(browseDataBackup);
|
2016-03-30 14:10:57 +02:00
|
|
|
browseData.remove();
|
|
|
|
|
|
|
|
QFile settings(profileDir.filePath(QSL("settings.ini")));
|
|
|
|
if (settings.exists()) {
|
|
|
|
const QString settingsBackup = QzTools::ensureUniqueFilename(profileDir.filePath(QSL("settings-backup.ini")));
|
|
|
|
settings.copy(settingsBackup);
|
|
|
|
settings.remove();
|
|
|
|
}
|
2014-03-09 21:51:42 +01:00
|
|
|
|
2018-01-22 14:15:14 +01:00
|
|
|
QFile sessionFile(profileDir.filePath(QSL("session.dat")));
|
|
|
|
if (sessionFile.exists() && !RestoreManager::validateFile(sessionFile.fileName())) {
|
|
|
|
QString oldVersion = QzTools::readAllFileContents(profileDir.filePath(QSL("version"))).trimmed();
|
|
|
|
if (oldVersion.isEmpty()) {
|
|
|
|
oldVersion = QSL("unknown-version");
|
|
|
|
}
|
|
|
|
const QString sessionBackup = QzTools::ensureUniqueFilename(profileDir.filePath(QSL("sessions/backup-%1.dat").arg(oldVersion)));
|
|
|
|
sessionFile.copy(sessionBackup);
|
|
|
|
sessionFile.remove();
|
|
|
|
}
|
|
|
|
|
2014-03-09 21:51:42 +01:00
|
|
|
const QString text = "Incompatible profile version has been detected. To avoid losing your profile data, they were "
|
|
|
|
"backed up in following file:<br/><br/><b>" + browseDataBackup + "<br/></b>";
|
2017-08-25 17:11:29 +02:00
|
|
|
QMessageBox::warning(0, "Falkon: Incompatible profile version", text);
|
2014-03-09 21:51:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProfileManager::connectDatabase()
|
|
|
|
{
|
|
|
|
QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"));
|
2017-11-13 13:25:30 +01:00
|
|
|
if (!db.isValid()) {
|
|
|
|
qCritical() << "Qt sqlite database driver is missing! Continuing without database....";
|
|
|
|
return;
|
2014-03-09 21:51:42 +01:00
|
|
|
}
|
|
|
|
|
2014-03-10 00:47:07 +01:00
|
|
|
if (mApp->isPrivate()) {
|
2014-03-09 21:51:42 +01:00
|
|
|
db.setConnectOptions("QSQLITE_OPEN_READONLY");
|
|
|
|
}
|
|
|
|
|
2017-11-13 13:25:30 +01:00
|
|
|
db.setDatabaseName(DataPaths::currentProfilePath() + QLatin1String("/browsedata.db"));
|
|
|
|
|
2014-03-09 21:51:42 +01:00
|
|
|
if (!db.open()) {
|
2017-11-13 13:25:30 +01:00
|
|
|
qCritical() << "Cannot open SQLite database! Continuing without database....";
|
|
|
|
return;
|
2014-03-09 21:51:42 +01:00
|
|
|
}
|
|
|
|
|
2017-11-13 13:25:30 +01:00
|
|
|
if (db.tables().isEmpty()) {
|
|
|
|
const QStringList statements = QzTools::readAllFileContents(QSL(":/data/browsedata.sql")).split(QL1C(';'));
|
|
|
|
for (const QString &statement : statements) {
|
|
|
|
const QString stmt = statement.trimmed();
|
|
|
|
if (stmt.isEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
QSqlQuery query;
|
|
|
|
if (!query.exec(stmt)) {
|
|
|
|
qCritical() << "Error creating database schema" << query.lastError().text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 12:49:35 +01:00
|
|
|
|
|
|
|
SqlDatabase::instance()->setDatabase(db);
|
2014-03-09 21:51:42 +01:00
|
|
|
}
|