Merge lp:~mardy/ubuntu-system-settings/lp1227690 into lp:ubuntu-system-settings

Proposed by Alberto Mardegan
Status: Merged
Approved by: Sebastien Bacher
Approved revision: 400
Merged at revision: 404
Proposed branch: lp:~mardy/ubuntu-system-settings/lp1227690
Merge into: lp:ubuntu-system-settings
Diff against target: 333 lines (+247/-13)
8 files modified
.bzrignore (+1/-0)
src/main.cpp (+2/-11)
src/system-settings.pro (+4/-2)
src/utils.cpp (+62/-0)
src/utils.h (+34/-0)
tests/tests.pro (+1/-0)
tests/tst_arguments.cpp (+112/-0)
tests/tst_arguments.pro (+31/-0)
To merge this branch: bzr merge lp:~mardy/ubuntu-system-settings/lp1227690
Reviewer Review Type Date Requested Status
Sebastien Bacher (community) Approve
PS Jenkins bot continuous-integration Approve
Review via email: mp+188284@code.launchpad.net

Commit message

Parse URLs given in command line

Description of the change

Parse URLs given in command line

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Sebastien Bacher (seb128) wrote :

looks fine and works as expected

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2013-08-02 12:08:52 +0000
3+++ .bzrignore 2013-09-30 09:32:01 +0000
4@@ -17,4 +17,5 @@
5 /lib/SystemSettings/SystemSettings.pc
6 /po/*.mo
7 /src/system-settings
8+/tests/tst_arguments
9 /tests/tst_plugins
10
11=== modified file 'src/main.cpp'
12--- src/main.cpp 2013-09-23 12:33:27 +0000
13+++ src/main.cpp 2013-09-30 09:32:01 +0000
14@@ -21,6 +21,7 @@
15 #include "debug.h"
16 #include "i18n.h"
17 #include "plugin-manager.h"
18+#include "utils.h"
19
20 #include <QGuiApplication>
21 #include <QProcessEnvironment>
22@@ -75,17 +76,7 @@
23 */
24 QString defaultPlugin;
25 QVariantMap pluginOptions;
26- QStringList arguments = app.arguments();
27- for (int i = 1; i < arguments.count(); i++) {
28- const QString &argument = arguments.at(i);
29- if (!argument.startsWith('-')) {
30- defaultPlugin = argument;
31- } else if (argument == "--option" && i + 1 < arguments.count()) {
32- QStringList option = arguments.at(++i).split("=");
33- // If no value is given, insert an empty string
34- pluginOptions.insert(option.at(0), option.value(1, ""));
35- }
36- }
37+ parsePluginOptions(app.arguments(), defaultPlugin, pluginOptions);
38
39 QQuickView view;
40 QObject::connect(view.engine(), SIGNAL(quit()), &app, SLOT(quit()),
41
42=== modified file 'src/system-settings.pro'
43--- src/system-settings.pro 2013-08-01 15:01:59 +0000
44+++ src/system-settings.pro 2013-09-30 09:32:01 +0000
45@@ -25,7 +25,8 @@
46 i18n.h \
47 item-model.h \
48 plugin-manager.h \
49- plugin.h
50+ plugin.h \
51+ utils.h
52
53 SOURCES = \
54 debug.cpp \
55@@ -33,7 +34,8 @@
56 item-model.cpp \
57 main.cpp \
58 plugin-manager.cpp \
59- plugin.cpp
60+ plugin.cpp \
61+ utils.cpp
62
63 QML_SOURCES = \
64 qml/CategoryGrid.qml \
65
66=== added file 'src/utils.cpp'
67--- src/utils.cpp 1970-01-01 00:00:00 +0000
68+++ src/utils.cpp 2013-09-30 09:32:01 +0000
69@@ -0,0 +1,62 @@
70+/*
71+ * This file is part of system-settings
72+ *
73+ * Copyright (C) 2013 Canonical Ltd.
74+ *
75+ * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
76+ *
77+ * This program is free software: you can redistribute it and/or modify it
78+ * under the terms of the GNU General Public License version 3, as published
79+ * by the Free Software Foundation.
80+ *
81+ * This program is distributed in the hope that it will be useful, but
82+ * WITHOUT ANY WARRANTY; without even the implied warranties of
83+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
84+ * PURPOSE. See the GNU General Public License for more details.
85+ *
86+ * You should have received a copy of the GNU General Public License along
87+ * with this program. If not, see <http://www.gnu.org/licenses/>.
88+ */
89+
90+#include "debug.h"
91+#include "utils.h"
92+
93+#include <QUrl>
94+#include <QUrlQuery>
95+
96+
97+typedef QPair<QString,QString> StringPair;
98+
99+namespace SystemSettings {
100+
101+void parsePluginOptions(const QStringList &arguments, QString &defaultPlugin,
102+ QVariantMap &pluginOptions)
103+{
104+ for (int i = 1; i < arguments.count(); i++) {
105+ const QString &argument = arguments.at(i);
106+ if (argument.startsWith("settings://")) {
107+ QUrl urlArgument(argument);
108+ /* Find out which plugin is required. If the first component of the
109+ * path is "system", just skip it. */
110+ QStringList pathComponents =
111+ urlArgument.path().split('/', QString::SkipEmptyParts);
112+ int pluginIndex = 0;
113+ if (pathComponents.value(pluginIndex, "") == "system")
114+ pluginIndex++;
115+ defaultPlugin = pathComponents.value(pluginIndex, QString());
116+ /* Convert the query parameters into options for the plugin */
117+ QUrlQuery query(urlArgument);
118+ Q_FOREACH(const StringPair &pair, query.queryItems()) {
119+ pluginOptions.insert(pair.first, pair.second);
120+ }
121+ } else if (!argument.startsWith('-')) {
122+ defaultPlugin = argument;
123+ } else if (argument == "--option" && i + 1 < arguments.count()) {
124+ QStringList option = arguments.at(++i).split("=");
125+ // If no value is given, insert an empty string
126+ pluginOptions.insert(option.at(0), option.value(1, ""));
127+ }
128+ }
129+}
130+
131+} // namespace
132
133=== added file 'src/utils.h'
134--- src/utils.h 1970-01-01 00:00:00 +0000
135+++ src/utils.h 2013-09-30 09:32:01 +0000
136@@ -0,0 +1,34 @@
137+/*
138+ * This file is part of system-settings
139+ *
140+ * Copyright (C) 2013 Canonical Ltd.
141+ *
142+ * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
143+ *
144+ * This program is free software: you can redistribute it and/or modify it
145+ * under the terms of the GNU General Public License version 3, as published
146+ * by the Free Software Foundation.
147+ *
148+ * This program is distributed in the hope that it will be useful, but
149+ * WITHOUT ANY WARRANTY; without even the implied warranties of
150+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
151+ * PURPOSE. See the GNU General Public License for more details.
152+ *
153+ * You should have received a copy of the GNU General Public License along
154+ * with this program. If not, see <http://www.gnu.org/licenses/>.
155+ */
156+
157+#ifndef SYSTEM_SETTINGS_UTILS_H
158+#define SYSTEM_SETTINGS_UTILS_H
159+
160+#include <QStringList>
161+#include <QVariantMap>
162+
163+namespace SystemSettings {
164+
165+void parsePluginOptions(const QStringList &arguments, QString &defaultPlugin,
166+ QVariantMap &pluginOptions);
167+
168+} // namespace
169+
170+#endif // SYSTEM_SETTINGS_UTILS_H
171
172=== modified file 'tests/tests.pro'
173--- tests/tests.pro 2013-05-09 10:32:25 +0000
174+++ tests/tests.pro 2013-09-30 09:32:01 +0000
175@@ -2,4 +2,5 @@
176 CONFIG += ordered
177 SUBDIRS = \
178 plugin.pro \
179+ tst_arguments.pro \
180 tst_plugins.pro
181
182=== added file 'tests/tst_arguments.cpp'
183--- tests/tst_arguments.cpp 1970-01-01 00:00:00 +0000
184+++ tests/tst_arguments.cpp 2013-09-30 09:32:01 +0000
185@@ -0,0 +1,112 @@
186+/*
187+ * This file is part of system-settings
188+ *
189+ * Copyright (C) 2013 Canonical Ltd.
190+ *
191+ * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
192+ *
193+ * This program is free software: you can redistribute it and/or modify it
194+ * under the terms of the GNU General Public License version 3, as published
195+ * by the Free Software Foundation.
196+ *
197+ * This program is distributed in the hope that it will be useful, but
198+ * WITHOUT ANY WARRANTY; without even the implied warranties of
199+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
200+ * PURPOSE. See the GNU General Public License for more details.
201+ *
202+ * You should have received a copy of the GNU General Public License along
203+ * with this program. If not, see <http://www.gnu.org/licenses/>.
204+ */
205+
206+#include "utils.h"
207+
208+#include <QDebug>
209+#include <QObject>
210+#include <QTest>
211+
212+using namespace SystemSettings;
213+
214+class ArgumentsTest: public QObject
215+{
216+ Q_OBJECT
217+
218+public:
219+ ArgumentsTest() {};
220+
221+private Q_SLOTS:
222+ void testNull();
223+ void testDefaultPlugin();
224+ void testPluginOptions();
225+ void testObsoleted();
226+};
227+
228+void ArgumentsTest::testNull()
229+{
230+ QStringList args(QStringLiteral("appName"));
231+ QString defaultPlugin;
232+ QVariantMap pluginOptions;
233+ parsePluginOptions(args, defaultPlugin, pluginOptions);
234+
235+ QCOMPARE(defaultPlugin, QString());
236+ QCOMPARE(pluginOptions, QVariantMap());
237+}
238+
239+void ArgumentsTest::testDefaultPlugin()
240+{
241+ QStringList args(QStringLiteral("appName"));
242+ args << QStringLiteral("settings://system/myPlugin");
243+
244+ QString defaultPlugin;
245+ QVariantMap pluginOptions;
246+ parsePluginOptions(args, defaultPlugin, pluginOptions);
247+
248+ QCOMPARE(defaultPlugin, QStringLiteral("myPlugin"));
249+ QCOMPARE(pluginOptions, QVariantMap());
250+
251+ /* Also check the host-less syntax */
252+ args = QStringList(QStringLiteral("appName"));
253+ args << QStringLiteral("settings:///system/myPlugin");
254+
255+ defaultPlugin.clear();
256+ parsePluginOptions(args, defaultPlugin, pluginOptions);
257+
258+ QCOMPARE(defaultPlugin, QStringLiteral("myPlugin"));
259+ QCOMPARE(pluginOptions, QVariantMap());
260+}
261+
262+void ArgumentsTest::testPluginOptions()
263+{
264+ QStringList args(QStringLiteral("appName"));
265+ args << QStringLiteral("settings:///system/myPlugin?greeting=hello&count=1");
266+
267+ QString defaultPlugin;
268+ QVariantMap pluginOptions;
269+ parsePluginOptions(args, defaultPlugin, pluginOptions);
270+
271+ QCOMPARE(defaultPlugin, QStringLiteral("myPlugin"));
272+ QVariantMap expectedOptions;
273+ expectedOptions.insert("greeting", QStringLiteral("hello"));
274+ expectedOptions.insert("count", QStringLiteral("1"));
275+ QCOMPARE(pluginOptions, expectedOptions);
276+}
277+
278+void ArgumentsTest::testObsoleted()
279+{
280+ QStringList args(QStringLiteral("appName"));
281+ args << QStringLiteral("myPlugin");
282+ args << QStringLiteral("--option") << QStringLiteral("greeting=hello");
283+ args << QStringLiteral("--option") << QStringLiteral("count=1");
284+
285+ QString defaultPlugin;
286+ QVariantMap pluginOptions;
287+ parsePluginOptions(args, defaultPlugin, pluginOptions);
288+
289+ QCOMPARE(defaultPlugin, QStringLiteral("myPlugin"));
290+ QVariantMap expectedOptions;
291+ expectedOptions.insert("greeting", QStringLiteral("hello"));
292+ expectedOptions.insert("count", QStringLiteral("1"));
293+ QCOMPARE(pluginOptions, expectedOptions);
294+}
295+
296+QTEST_MAIN(ArgumentsTest);
297+#include "tst_arguments.moc"
298
299=== added file 'tests/tst_arguments.pro'
300--- tests/tst_arguments.pro 1970-01-01 00:00:00 +0000
301+++ tests/tst_arguments.pro 2013-09-30 09:32:01 +0000
302@@ -0,0 +1,31 @@
303+include(../common-project-config.pri)
304+include($${TOP_SRC_DIR}/common-vars.pri)
305+
306+TARGET = tst_arguments
307+
308+CONFIG += \
309+ debug
310+
311+QT += \
312+ core \
313+ qml \
314+ testlib
315+
316+SRC_DIR = $$TOP_SRC_DIR/src
317+SOURCES += \
318+ tst_arguments.cpp \
319+ $$SRC_DIR/utils.cpp
320+HEADERS += \
321+ $$SRC_DIR/utils.cpp
322+
323+INCLUDEPATH += \
324+ . \
325+ $$SRC_DIR
326+
327+DEFINES += \
328+ DEBUG_ENABLED \
329+ UNIT_TESTS
330+
331+check.commands = "xvfb-run -a ./$$TARGET"
332+check.depends = $$TARGET
333+QMAKE_EXTRA_TARGETS += check

Subscribers

People subscribed via source and target branches