diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ Core Qml DBus + Network ) add_subdirectory(plugin) diff --git a/plasmoid/contents/ui/main.qml b/plasmoid/contents/ui/main.qml --- a/plasmoid/contents/ui/main.qml +++ b/plasmoid/contents/ui/main.qml @@ -74,11 +74,21 @@ property alias recipeReadLmodel: recipeReadListModel property bool intentfailure: false property bool locationUserSelected: false + property bool connectCtx: false property var geoLat property var geoLong property var globalcountrycode property var weatherMetric: "metric" + Connections { + target: plasmoid + onExpandedChanged: { + if (plasmoid.expanded) { + checkDashStatus() + } + } + } + Connections { target: main2 ignoreUnknownSignals: true @@ -109,6 +119,26 @@ } } + function checkDashStatus(){ + if(dashListModel.count == 0){ + checkConnectionStatus() + } + } + + function checkConnectionStatus(){ + var isConnected = PlasmaLa.ConnectionCheck.checkConnection() + if(!isConnected){ + if(!connectCtx){ + var conError = i18n("I am not connected to the 🌐 internet, Please check your network connection") + convoLmodel.append({"itemType": "NonVisual", "InputQuery": conError}); + connectCtx = true + } + } + else { + geoDataSource.connectedSources = ["location"] + } + } + function toggleInputMethod(selection){ switch(selection){ case "KeyboardSetActive": @@ -445,20 +475,24 @@ } PlasmaCore.DataSource { - id: dataSource - dataEngine: "geolocation" - connectedSources: ["location"] - - onNewData: { - if (sourceName == "location"){ - geoLat = data.latitude - geoLong = data.longitude - var globalcountry = data.country - globalcountrycode = globalcountry.substring(0, 2) - showDash("setVisible") - } + id: geoDataSource + dataEngine: "geolocation" + + onSourceAdded: { + connectSource(source) + } + + onNewData: { + convoLmodel.clear() + if (sourceName == "location"){ + geoLat = data.latitude + geoLong = data.longitude + var globalcountry = data.country + globalcountrycode = globalcountry.substring(0, 2) + showDash("setVisible") } - } + } + } ListModel { id: dashListModel diff --git a/plugin/CMakeLists.txt b/plugin/CMakeLists.txt --- a/plugin/CMakeLists.txt +++ b/plugin/CMakeLists.txt @@ -1,10 +1,10 @@ kde_enable_exceptions() -set(mycroftplasmoidplugin_SRCS mycroftplasmoidplugin.cpp launchapp.cpp notify.cpp filereader.cpp msmapp.cpp mycroftplasmoid_dbus.cpp) +set(mycroftplasmoidplugin_SRCS mycroftplasmoidplugin.cpp launchapp.cpp notify.cpp filereader.cpp msmapp.cpp connectioncheck.cpp mycroftplasmoid_dbus.cpp) add_library(mycroftplasmoidplugin SHARED ${mycroftplasmoidplugin_SRCS}) -target_link_libraries(mycroftplasmoidplugin Qt5::Gui Qt5::Core Qt5::Qml Qt5::DBus KF5::Plasma KF5::I18n KF5::Notifications) +target_link_libraries(mycroftplasmoidplugin Qt5::Gui Qt5::Core Qt5::Qml Qt5::DBus Qt5::Network KF5::Plasma KF5::I18n KF5::Notifications) install(TARGETS mycroftplasmoidplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/private/mycroftplasmoid) diff --git a/plugin/connectioncheck.h b/plugin/connectioncheck.h new file mode 100644 --- /dev/null +++ b/plugin/connectioncheck.h @@ -0,0 +1,38 @@ + /* + * Copyright (C) 2016 by Aditya Mehra * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef CONNECTIONCHECK_H +#define CONNECTIONCHECK_H + +#include +#include +#include + +class ConnectionCheck : public QObject +{ + Q_OBJECT + +public: + explicit ConnectionCheck(QObject *parent = Q_NULLPTR); + +public Q_SLOTS: + bool checkConnection(); + +}; + +#endif // CONNECTIONCHECK_H diff --git a/plugin/connectioncheck.cpp b/plugin/connectioncheck.cpp new file mode 100644 --- /dev/null +++ b/plugin/connectioncheck.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2016 by Aditya Mehra * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2, 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 Library General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "connectioncheck.h" + +#include + +ConnectionCheck::ConnectionCheck(QObject *parent) + : QObject(parent) +{ +} + +bool ConnectionCheck::checkConnection(){ + QNetworkAccessManager nam; + QNetworkRequest req(QUrl("http://www.example.com/")); + QNetworkReply *reply = nam.get(req); + QEventLoop loop; + connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + if(reply->bytesAvailable()) { + return true; + } + else { + return false; + } +} diff --git a/plugin/mycroftplasmoidplugin.cpp b/plugin/mycroftplasmoidplugin.cpp --- a/plugin/mycroftplasmoidplugin.cpp +++ b/plugin/mycroftplasmoidplugin.cpp @@ -22,6 +22,7 @@ #include "notify.h" #include "filereader.h" #include "msmapp.h" +#include "connectioncheck.h" #include #include #include @@ -50,12 +51,21 @@ return new FileReader; } +static QObject *connectioncheck_singleton(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + return new ConnectionCheck; +} + void MycroftPlasmoidPlugin::registerTypes(const char *uri) { Q_ASSERT(uri == QLatin1String("org.kde.private.mycroftplasmoid")); qmlRegisterSingletonType(uri, 1, 0, "Notify", notify_singleton); qmlRegisterSingletonType(uri, 1, 0, "LaunchApp", launchapp_singleton); - qmlRegisterSingletonType(uri, 1, 0, "FileReader", filereader_singleton); + qmlRegisterSingletonType(uri, 1, 0, "FileReader", filereader_singleton); + qmlRegisterSingletonType(uri, 1, 0, "ConnectionCheck", connectioncheck_singleton); qmlRegisterType(uri, 1, 0, "MsmApp"); }