]> rtime.felk.cvut.cz Git - coffee/qtwebbrowser.git/blob - src/engine.cpp
make it possible to pass URLs on command line
[coffee/qtwebbrowser.git] / src / engine.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtBrowser project.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 2 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.GPLv2 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU General Public License version 2 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 3.0 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 3.0 requirements will be
31 ** met: http://www.gnu.org/copyleft/gpl.html.
32 **
33 **
34 ** $QT_END_LICENSE$
35 **
36 ****************************************************************************/
37
38 #include "engine.h"
39
40 #include <QtCore/QDir>
41 #include <QtCore/QStandardPaths>
42 #include <QStringBuilder>
43 #include <QCoreApplication>
44
45 Engine::Engine(QObject *parent)
46     : QObject(parent)
47     , m_settings(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) % QDir::separator() % "settings.ini", QSettings::IniFormat, this)
48 {
49     foreach (const QString &arg, QCoreApplication::arguments().mid(1)) {
50         if (arg.startsWith('-'))
51             continue;
52         const QUrl url(arg);
53         if (url.isValid()) {
54             m_initialUrl = url.toString();
55             break;
56         }
57     }
58 }
59
60 QString Engine::settingsPath()
61 {
62     return m_settings.fileName();
63 }
64
65 QString Engine::initialUrl() const
66 {
67     return m_initialUrl;
68 }
69
70 QUrl Engine::fromUserInput(const QString& userInput)
71 {
72     QFileInfo fileInfo(userInput);
73     if (fileInfo.exists())
74         return QUrl::fromLocalFile(fileInfo.absoluteFilePath());
75     return QUrl::fromUserInput(userInput);
76 }
77
78 bool Engine::isUrl(const QString& userInput)
79 {
80     if (userInput.startsWith(QStringLiteral("www."))
81             || userInput.startsWith(QStringLiteral("http"))
82             || userInput.startsWith(QStringLiteral("ftp"))
83             || userInput.contains(QStringLiteral("://"))
84             || userInput.endsWith(QStringLiteral(".com")))
85         return true;
86     return false;
87 }
88
89 QString Engine::domainFromString(const QString& urlString)
90 {
91     return QUrl::fromUserInput(urlString).host();
92 }
93
94 QString Engine::fallbackColor()
95 {
96     static QList<QString> colors = QList<QString>() << QStringLiteral("#46a2da")
97                                                     << QStringLiteral("#18394c")
98                                                     << QStringLiteral("#ff8c0a")
99                                                     << QStringLiteral("#5caa15");
100     static int index = -1;
101     if (++index == colors.count())
102         index = 0;
103     return colors[index];
104 }
105
106 QString Engine::restoreSetting(const QString &name, const QString &defaultValue)
107 {
108     return m_settings.value(name, defaultValue).toString();
109 }
110
111 void Engine::saveSetting(const QString &name, const QString &value)
112 {
113     m_settings.setValue(name, value);
114 }