Merge lp:~jamesh/mediascanner2/qml-no-db-available into lp:mediascanner2

Proposed by James Henstridge
Status: Merged
Approved by: Michi Henning
Approved revision: 320
Merged at revision: 319
Proposed branch: lp:~jamesh/mediascanner2/qml-no-db-available
Merge into: lp:mediascanner2
Diff against target: 135 lines (+67/-6)
5 files modified
src/extractor/CMakeLists.txt (+5/-1)
src/qml/Ubuntu/MediaScanner.0.1/MediaStoreWrapper.cc (+18/-4)
src/qml/Ubuntu/MediaScanner.0.1/StreamingModel.cc (+1/-1)
test/CMakeLists.txt (+5/-0)
test/test_qml_nodb.qml (+38/-0)
To merge this branch: bzr merge lp:~jamesh/mediascanner2/qml-no-db-available
Reviewer Review Type Date Requested Status
Michi Henning (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+280422@code.launchpad.net

Commit message

If the mediascanner index can not be opened, catch the exception in the QML plugin and act as if the database is empty. A warning is printed via Qt's logging framework. This prevents QML apps using mediascanner from terminating if there is a problem opening the media index.

Description of the change

If the mediascanner index can not be opened, allow construction of the QML MediaScannerWrapper class to succeed. Letting the exception propagate causes QML apps to terminate, so is not a helpful behaviour.

With this change, we now:

1. print a warning on construction.
2. print a warning if the MediaStoreWrapper's lookup() or query() methods are called and return zero/empty results.
3. Make the various model classes behave as if they are empty.

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
Michi Henning (michihenning) wrote :

Looks great!

review: Approve
Revision history for this message
Michi Henning (michihenning) wrote :

Cool thanks!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/extractor/CMakeLists.txt'
2--- src/extractor/CMakeLists.txt 2015-10-22 08:19:07 +0000
3+++ src/extractor/CMakeLists.txt 2015-12-15 04:28:24 +0000
4@@ -12,7 +12,11 @@
5 OUTPUT dbus-generated.c dbus-generated.h
6 COMMAND ${gdbus_codegen} --interface-prefix=com.canonical.MediaScanner2 --generate-c-code dbus-generated --c-namespace MS ${CMAKE_CURRENT_SOURCE_DIR}/dbus-interface.xml
7 MAIN_DEPENDENCY dbus-interface.xml
8-)
9+ )
10+# Code generated by gdbus-codegen doesn't like all the warning flags
11+# we have turned on.
12+set_property(SOURCE dbus-generated.c APPEND_STRING PROPERTY
13+ COMPILE_FLAGS " -Wno-unused-parameter -Wno-pedantic")
14
15 # The client code for the extractor daemon
16 add_library(extractor-client STATIC
17
18=== modified file 'src/qml/Ubuntu/MediaScanner.0.1/MediaStoreWrapper.cc'
19--- src/qml/Ubuntu/MediaScanner.0.1/MediaStoreWrapper.cc 2014-08-21 03:55:40 +0000
20+++ src/qml/Ubuntu/MediaScanner.0.1/MediaStoreWrapper.cc 2015-12-15 04:28:24 +0000
21@@ -46,10 +46,14 @@
22 MediaStoreWrapper::MediaStoreWrapper(QObject *parent)
23 : QObject(parent) {
24 const char *use_dbus = getenv("MEDIASCANNER_USE_DBUS");
25- if (use_dbus != nullptr && !strcmp(use_dbus, "1")) {
26- store.reset(new mediascanner::dbus::ServiceStub(the_session_bus()));
27- } else {
28- store.reset(new mediascanner::MediaStore(MS_READ_ONLY));
29+ try {
30+ if (use_dbus != nullptr && !strcmp(use_dbus, "1")) {
31+ store.reset(new mediascanner::dbus::ServiceStub(the_session_bus()));
32+ } else {
33+ store.reset(new mediascanner::MediaStore(MS_READ_ONLY));
34+ }
35+ } catch (const std::exception &e) {
36+ qWarning() << "Could not initialise media store:" << e.what();
37 }
38
39 QDBusConnection::sessionBus().connect(
40@@ -61,6 +65,11 @@
41 }
42
43 QList<QObject*> MediaStoreWrapper::query(const QString &q, MediaType type) {
44+ if (!store) {
45+ qWarning() << "query() called on invalid MediaStore";
46+ return QList<QObject*>();
47+ }
48+
49 QList<QObject*> result;
50 try {
51 for (const auto &media : store->query(q.toStdString(), static_cast<mediascanner::MediaType>(type), mediascanner::Filter())) {
52@@ -75,6 +84,11 @@
53 }
54
55 MediaFileWrapper *MediaStoreWrapper::lookup(const QString &filename) {
56+ if (!store) {
57+ qWarning() << "lookup() called on invalid MediaStore";
58+ return nullptr;
59+ }
60+
61 MediaFileWrapper *wrapper;
62 try {
63 wrapper = new MediaFileWrapper(store->lookup(filename.toStdString()));
64
65=== modified file 'src/qml/Ubuntu/MediaScanner.0.1/StreamingModel.cc'
66--- src/qml/Ubuntu/MediaScanner.0.1/StreamingModel.cc 2014-08-15 11:05:47 +0000
67+++ src/qml/Ubuntu/MediaScanner.0.1/StreamingModel.cc 2015-12-15 04:28:24 +0000
68@@ -104,7 +104,7 @@
69 }
70
71 void StreamingModel::updateModel() {
72- if (store.isNull()) {
73+ if (store.isNull() || !store->store) {
74 query_future = QFuture<void>();
75 setStatus(Ready);
76 return;
77
78=== modified file 'test/CMakeLists.txt'
79--- test/CMakeLists.txt 2015-10-22 10:13:51 +0000
80+++ test/CMakeLists.txt 2015-12-15 04:28:24 +0000
81@@ -78,6 +78,11 @@
82 ENVIRONMENT "QT_QPA_PLATFORM=minimal;MEDIASCANNER_USE_DBUS=1"
83 TIMEOUT 600)
84
85+add_test(test_qml_nodb qmltestrunner -import ${CMAKE_BINARY_DIR}/src/qml -input ${CMAKE_CURRENT_SOURCE_DIR}/test_qml_nodb.qml)
86+set_tests_properties(test_qml_nodb PROPERTIES
87+ ENVIRONMENT "QT_QPA_PLATFORM=minimal;MEDIASCANNER_CACHEDIR=${CMAKE_CURRENT_BINARY_DIR}/qml-nodb-cachedir;DBUS_SESSION_BUS_ADDRESS="
88+ TIMEOUT 600)
89+
90
91 add_executable(test_util test_util.cc ../src/mediascanner/utils.cc)
92 target_link_libraries(test_util gtest ${GLIB_LDFLAGS})
93
94=== added file 'test/test_qml_nodb.qml'
95--- test/test_qml_nodb.qml 1970-01-01 00:00:00 +0000
96+++ test/test_qml_nodb.qml 2015-12-15 04:28:24 +0000
97@@ -0,0 +1,38 @@
98+import QtQuick 2.0
99+import QtTest 1.0
100+import Ubuntu.MediaScanner 0.1
101+
102+TestCase {
103+ id: root
104+ name: "NoDatabaseTests"
105+
106+ function test_mediastore() {
107+ ignoreWarning("Could not initialise media store: unable to open database file");
108+ var store = Qt.createQmlObject(
109+ "import Ubuntu.MediaScanner 0.1;" +
110+ "MediaStore {}", root);
111+ if (store === null) {
112+ fail("Could not create MediaStore component");
113+ }
114+
115+ ignoreWarning("query() called on invalid MediaStore");
116+ compare(store.query("foo", MediaStore.AllMedia), []);
117+
118+ ignoreWarning("lookup() called on invalid MediaStore");
119+ compare(store.lookup("/some/file"), null);
120+ }
121+
122+ function test_songsmodel() {
123+ ignoreWarning("Could not initialise media store: unable to open database file");
124+ var model = Qt.createQmlObject(
125+ "import Ubuntu.MediaScanner 0.1;" +
126+ "SongsModel {" +
127+ " store: MediaStore {}" +
128+ "}", root);
129+ if (model === null) {
130+ fail("Could not create SongsModel component");
131+ }
132+ // Model is empty
133+ compare(model.count, 0);
134+ }
135+}

Subscribers

People subscribed via source and target branches