diff --git a/cmake/FindLIBFWUPD.cmake b/cmake/FindLIBFWUPD.cmake index 2b592cac..b53a5fe9 100644 --- a/cmake/FindLIBFWUPD.cmake +++ b/cmake/FindLIBFWUPD.cmake @@ -1,58 +1,63 @@ # - Try to find the Fwupd library # Once done this will define # # LIBFWUPD_FOUND - system has the fwupd library # LIBFWUPD_INCLUDE_DIR - the Fwupd include directory # LIBFWUPD_LIBRARY - Link this to use the fwupd # # Copyright © 2018, Abhijeet Sharma # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # 3. Neither the name of the University nor the names of its contributors # may be used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. if(LIBFWUPD_INCLUDE_DIRS AND LIBFWUPD_LIBRARIES) set(LIBFWUPD_FOUND TRUE) else (LIBFWUPD_INCLUDE_DIRS AND LIBFWUPD_LIBRARIES) - find_library (LIBFWUPD_LIBRARIES - NAMES fwupd libfwupd - ) + find_library (LIBFWUPD_LIBRARIES + NAMES fwupd libfwupd + ) - find_path (LIBFWUPD_INCLUDE_DIRS - NAMES fwupd.h - PATH_SUFFIXES fwupd-1 - HINTS fwupd-1/libfwupd - ) - set(LIBFWUPD_FOUND TRUE) + find_path (LIBFWUPD_INCLUDE_DIRS + NAMES fwupd.h + PATH_SUFFIXES fwupd-1 + HINTS fwupd-1/libfwupd + ) + set(LIBFWUPD_FOUND TRUE) + find_package(GIO) + find_package(Soup) + find_package(GLib) + endif (LIBFWUPD_INCLUDE_DIRS AND LIBFWUPD_LIBRARIES) if (LIBFWUPD_FOUND) - add_library(LIBFWUPD SHARED IMPORTED) - set_target_properties(LIBFWUPD PROPERTIES + add_library(LIBFWUPD SHARED IMPORTED) + set_target_properties(LIBFWUPD PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${LIBFWUPD_INCLUDE_DIRS} IMPORTED_LOCATION ${LIBFWUPD_LIBRARIES} ) + add_dependencies(LIBFWUPD GIO Soup GLib) endif() diff --git a/libdiscover/backends/FwupdBackend/CMakeLists.txt b/libdiscover/backends/FwupdBackend/CMakeLists.txt index 5d8523f6..3b2d7791 100644 --- a/libdiscover/backends/FwupdBackend/CMakeLists.txt +++ b/libdiscover/backends/FwupdBackend/CMakeLists.txt @@ -1,42 +1,27 @@ add_subdirectory(tests) add_definitions( -DPROJECT_NAME=${PROJECT_NAME} -DPROJECT_VERSION=${PROJECT_VERSION}) set(fwupd-backend_SRCS FwupdResource.cpp FwupdBackend.cpp - FwupdReviewsBackend.cpp FwupdTransaction.cpp FwupdSourcesBackend.cpp FwupdUpdater.cpp ) - - - -#find_package(GIO) -find_package(Soup) -find_package(GLib) - - -MESSAGE( STATUS "FWUPD_INCLUDE_DIRS variable is: " ${LIBFWUPD_INCLUDE_DIRS} ) -MESSAGE( STATUS "FWUPD_LIBRARIES variable is: " ${LIBFWUPD_LIBRARIES} ) +find_package(GIO) include_directories(${LIBFWUPD_INCLUDE_DIRS} ) - - - add_library(fwupd-backend MODULE ${fwupd-backend_SRCS}) target_link_libraries(fwupd-backend Qt5::Core Qt5::Widgets KF5::CoreAddons KF5::ConfigCore Discover::Common LIBFWUPD ${Soup} ${GIO} ${GLib} ) - - install(TARGETS fwupd-backend DESTINATION ${PLUGIN_INSTALL_DIR}/discover) -install(FILES fwupd-backend-categories.xml DESTINATION ${DATA_INSTALL_DIR}/libdiscover/categories) +#install(FILES fwupd-backend-categories.xml DESTINATION ${DATA_INSTALL_DIR}/libdiscover/categories) add_library(FwupdNotifier MODULE FwupdNotifier.cpp ) target_link_libraries(FwupdNotifier Discover::Notifiers) set_target_properties(FwupdNotifier PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_FULL_LIBDIR}/plasma-discover) install(TARGETS FwupdNotifier DESTINATION ${PLUGIN_INSTALL_DIR}/discover-notifier) diff --git a/libdiscover/backends/FwupdBackend/FwupdBackend.cpp b/libdiscover/backends/FwupdBackend/FwupdBackend.cpp index 61100c6d..9a279cd5 100644 --- a/libdiscover/backends/FwupdBackend/FwupdBackend.cpp +++ b/libdiscover/backends/FwupdBackend/FwupdBackend.cpp @@ -1,858 +1,757 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #include "FwupdBackend.h" #include "FwupdResource.h" -#include "FwupdReviewsBackend.h" #include "FwupdTransaction.h" #include "FwupdSourcesBackend.h" #include #include #include #include #include #include #include #include -#include -#include -#include -#include -#include - DISCOVER_BACKEND_PLUGIN(FwupdBackend) - -#define STRING(s) #s // For Project Name and Version FwupdBackend::FwupdBackend(QObject* parent) : AbstractResourcesBackend(parent) , m_updater(new FwupdUpdater(this)) - , m_reviews(new FwupdReviewsBackend(this)) , m_fetching(true) { QTimer::singleShot(500, this, &FwupdBackend::toggleFetching); - connect(m_reviews, &FwupdReviewsBackend::ratingsReady, this, &AbstractResourcesBackend::emitRatingsReady); connect(m_updater, &FwupdUpdater::updatesCountChanged, this, &FwupdBackend::updatesCountChanged); client = fwupd_client_new (); - toDownload = g_ptr_array_new_with_free_func (g_free); - toIgnore = g_ptr_array_new_with_free_func (g_free); - /* use a custom user agent to provide the fwupd version */ - userAgent = fwupd_build_user_agent (STRING(PROJECT_NAME),STRING(PROJECT_VERSION)); - soupSession = soup_session_new_with_options (SOUP_SESSION_USER_AGENT, userAgent,SOUP_SESSION_TIMEOUT, 10,NULL); - soup_session_remove_feature_by_type (soupSession,SOUP_TYPE_CONTENT_DECODER); - - if (!m_fetching) - m_reviews->initialize(); populate(QStringLiteral("Releases")); - SourcesModel::global()->addSourcesBackend(new FwupdSourcesBackend(this)); } +QMap FwupdBackend::initHashMap() +{ + QMap map; + + map.insert(G_CHECKSUM_SHA1,QCryptographicHash::Sha1); + map.insert(G_CHECKSUM_SHA256,QCryptographicHash::Sha256); + map.insert(G_CHECKSUM_SHA512,QCryptographicHash::Sha512); + map.insert(G_CHECKSUM_MD5,QCryptographicHash::Md5); + + return map; +} + FwupdBackend::~FwupdBackend() { g_object_unref (client); - g_ptr_array_unref (toDownload); - g_ptr_array_unref (toIgnore); + toDownload.clear(); + toIgnore.clear(); } -gchar* FwupdBackend::FwupdBuildDeviceID(FwupdDevice* device) +QString FwupdBackend::FwupdBuildDeviceID(FwupdDevice* device) { - g_autofree gchar *tmp = g_strdup (fwupd_device_get_id (device)); - g_strdelimit (tmp, "/", '_'); - return g_strdup_printf ("org.fwupd.%s.device", tmp); + QString DeviceID = QLatin1String(fwupd_device_get_id (device)); + DeviceID.replace(QLatin1Char('/'),QLatin1Char('_')); + return QStringLiteral("org.fwupd.%1.device").arg(DeviceID); } QString FwupdBackend::FwupdGetAppName(QString ID) { //To Do Implement it! return ID; } QSet FwupdBackend::FwupdGetAllUpdates() { QSet ret; ret.reserve(m_toUpdate.size()); foreach(FwupdResource* r, m_toUpdate) { AbstractResource* res = (AbstractResource*) r; if(r->m_id.isEmpty()) qDebug() << "Resource ID is Empty" << r->m_name; ret.insert(res); } return ret; } FwupdResource * FwupdBackend::FwupdCreateDevice(FwupdDevice *device) { const QString name = QLatin1String(fwupd_device_get_name(device)); FwupdResource* res = new FwupdResource(name, true, this); - res->setId(QLatin1String(FwupdBuildDeviceID(device))); + res->setId(FwupdBuildDeviceID(device)); res->addCategories(QStringLiteral("Releases")); res->setIconName(QLatin1String((const gchar *)g_ptr_array_index (fwupd_device_get_icons(device),0)));// Implement a Better way to decide icon FwupdSetDeviceDetails(res,device); return res; } FwupdResource * FwupdBackend::FwupdCreateRelease(FwupdDevice *device) { FwupdRelease *rel = fwupd_device_get_release_default (device); const QString name = QLatin1String(fwupd_release_get_name(rel)); FwupdResource* res = new FwupdResource(name, true, this); res->setDeviceID(QLatin1String(fwupd_device_get_id (device))); FwupdSetReleaseDetails(res,rel); FwupdSetDeviceDetails(res,device); - if (fwupd_release_get_appstream_id (rel) != NULL) + if (fwupd_release_get_appstream_id (rel)) res->setId(QLatin1String(fwupd_release_get_appstream_id (rel))); /* the same as we have already */ - if (g_strcmp0 (fwupd_device_get_version (device),fwupd_release_get_version (rel)) == 0) + if(QLatin1Literal(fwupd_device_get_version (device)) == QLatin1Literal(fwupd_release_get_version (rel))) { - qWarning() << "same firmware version as installed"; + qWarning() << "same firmware version as installed"; } return res; } void FwupdBackend::FwupdSetReleaseDetails(FwupdResource *res,FwupdRelease *rel) { res->addCategories(QLatin1String("Releases")); if(fwupd_release_get_summary(rel)) res->setSummary(QLatin1String(fwupd_release_get_summary(rel))); - if(fwupd_release_get_vendor(rel) != NULL) + if(fwupd_release_get_vendor(rel)) res->setVendor(QLatin1String(fwupd_release_get_vendor(rel))); - if(fwupd_release_get_version(rel) != NULL) + if(fwupd_release_get_version(rel)) res->setVersion(QLatin1String(fwupd_release_get_version(rel))); - if(fwupd_release_get_description(rel) != NULL) + if(fwupd_release_get_description(rel)) res->setDescription(QLatin1String((fwupd_release_get_description (rel)))); - if(fwupd_release_get_homepage(rel) != NULL) + if(fwupd_release_get_homepage(rel)) res->setHomePage(QUrl(QLatin1String(fwupd_release_get_homepage(rel)))); if(fwupd_release_get_license(rel)) res->setLicense(QLatin1String(fwupd_release_get_license(rel))); - if (fwupd_release_get_uri (rel) != NULL) - { + if (fwupd_release_get_uri (rel)) res->m_updateURI = QLatin1String(fwupd_release_get_uri (rel)); - } } void FwupdBackend::FwupdSetDeviceDetails(FwupdResource *res,FwupdDevice *dev) { - GPtrArray *guids; - if (fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE)) - res->isLiveUpdatable = true; - if (fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_ONLY_OFFLINE)) - res->isOnlyOffline = true; - if (fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_REBOOT)) - res->needsReboot = true; - if (!fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_INTERNAL)) - res->isDeviceRemoval = true; - guids = fwupd_device_get_guids (dev); + res->isLiveUpdatable = fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE); + res->isOnlyOffline = fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_ONLY_OFFLINE); + res->needsReboot = fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_NEEDS_REBOOT); + res->isDeviceRemoval = !fwupd_device_has_flag (dev, FWUPD_DEVICE_FLAG_INTERNAL); + + GPtrArray *guids = fwupd_device_get_guids (dev); if(guids->len > 0) { - g_autofree gchar *guid_str = NULL; - g_auto(GStrv) tmp = g_new0 (gchar *, guids->len + 1); - for (int i = 0; i < (int)guids->len; i++) - tmp[i] = g_strdup ((gchar *)g_ptr_array_index (guids, i)); - guid_str = g_strjoinv (",", tmp); - res->guidString = guid_str; - } - if(fwupd_device_get_name (dev) != NULL) - { - g_autofree gchar *vendor_name = NULL; - if (g_str_has_prefix (fwupd_device_get_name (dev),fwupd_device_get_vendor (dev))) + QString guidStr = QLatin1Literal((char *)g_ptr_array_index (guids, 0)); + for (uint i = 1; i < guids->len; i++) { - vendor_name = g_strdup (fwupd_device_get_name (dev)); + guidStr += QLatin1Char(',') + QLatin1Literal((char *)g_ptr_array_index (guids, i)); } - else - { - vendor_name = g_strdup_printf ("%s %s",fwupd_device_get_vendor (dev), fwupd_device_get_name (dev)); - } - res->setName(QLatin1String(vendor_name)); + res->guidString = guidStr; + } + if(fwupd_device_get_name (dev)) + { + QString vendorName; + vendorName.sprintf("%s",fwupd_device_get_name (dev)); + + if(vendorName.indexOf(QLatin1String(fwupd_device_get_vendor (dev))) == 0) + vendorName.sprintf("%s %s",fwupd_device_get_vendor (dev), fwupd_device_get_name (dev)); + res->setName(vendorName); } - if(fwupd_device_get_summary (dev) != NULL) + if(fwupd_device_get_summary (dev)) res->setSummary(QLatin1String(fwupd_device_get_summary(dev))); - if(fwupd_device_get_vendor(dev) != NULL) + if(fwupd_device_get_vendor(dev)) res->setVendor(QLatin1String(fwupd_device_get_vendor(dev))); - if(fwupd_device_get_version(dev) != NULL) + if(fwupd_device_get_version(dev)) res->setVersion(QLatin1String(fwupd_device_get_version(dev))); - if(fwupd_device_get_description(dev) != NULL) + if(fwupd_device_get_description(dev)) res->setDescription(QLatin1String((fwupd_device_get_description(dev)))); } void FwupdBackend::populate(const QString& n) { - g_autoptr(GPtrArray) devices = NULL; - /* get devices */ - devices = fwupd_client_get_devices (client, NULL, NULL); + g_autoptr(GPtrArray) devices = fwupd_client_get_devices (client, NULL, NULL); - if (devices != NULL) + if (devices) { - for (int i = 0; i < (int)devices->len; i++) + for (uint i = 0; i < devices->len; i++) { FwupdDevice *device = (FwupdDevice *)g_ptr_array_index (devices, i); - FwupdResource * res = NULL; - g_autoptr(GPtrArray) releases = NULL; /* Devices Which are not updatable */ - // if (!fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE)) - // continue; + if (!fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_UPDATABLE)) + continue; /* add releases */ - res = FwupdCreateDevice(device); + FwupdResource * res = FwupdCreateDevice(device); res->addCategories(n); - releases = fwupd_client_get_releases (client,res->m_deviceID.toUtf8().constData(),NULL,NULL); + g_autoptr(GPtrArray) releases = fwupd_client_get_releases (client,res->m_deviceID.toUtf8().constData(),NULL,NULL); - if (releases != NULL) + if (releases) { for (int j = 0; j < (int)releases->len; j++) { FwupdRelease *rel = (FwupdRelease *)g_ptr_array_index (releases, j); const QString name = QLatin1String(fwupd_release_get_name(rel)); FwupdResource* res_ = new FwupdResource(name, true, this); FwupdSetReleaseDetails (res_, rel); res->m_releases.append(res_); } } /* add all Valid Resources */ m_resources.insert(res->name().toLower(), res); } } } void FwupdBackend::FwupdAddUpdates() { - g_autoptr(GPtrArray) remotes = NULL; g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; g_autoptr(GError) error2 = NULL; - g_autoptr(GPtrArray) devices = NULL; + g_autoptr(GPtrArray) devices = fwupd_client_get_devices (client, cancellable, &error); g_autoptr(GPtrArray) rels = NULL; - - /* get All devices, Will filter latter */ - devices = fwupd_client_get_devices (client, cancellable, &error); - if(devices == NULL){ - if (g_error_matches (error,FWUPD_ERROR,FWUPD_ERROR_NOTHING_TO_DO)){ + + if(!devices) + { + if (g_error_matches (error,FWUPD_ERROR,FWUPD_ERROR_NOTHING_TO_DO)) + { #ifdef FWUPD_DEBUG qDebug() << "No Devices Found"; #endif FwupdHandleError(&error); } } else{ - for (int i = 0; i < (int)devices->len; i++) { + for (uint i = 0; i < devices->len; i++) + { FwupdDevice *device = (FwupdDevice *)g_ptr_array_index (devices, i); FwupdResource* res; - res = FwupdCreateDevice(device); //just to test code should be deleted - m_toUpdate.append(res); //just to test code should be deleted + res = FwupdCreateDevice(device); //just to test code should be deleted + if(!m_toUpdate.contains(res)) // Just To Test code Should Be deleted + m_toUpdate.append(res); //just to test code should be deleted if (!fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_SUPPORTED)) continue; /*Device is Locked Needs Unlocking*/ if (fwupd_device_has_flag (device, FWUPD_DEVICE_FLAG_LOCKED)) { - res = FwupdCreateDevice(device); - res->setIsDeviceLocked(true); - m_toUpdate.append(res); - connect(res, &FwupdResource::stateChanged, this, &FwupdBackend::updatesCountChanged); - continue; + res = FwupdCreateDevice(device); + res->setIsDeviceLocked(true); + if(!m_toUpdate.contains(res)) + m_toUpdate.append(res); + connect(res, &FwupdResource::stateChanged, this, &FwupdBackend::updatesCountChanged); + continue; } rels = fwupd_client_get_upgrades (client,fwupd_device_get_id(device),cancellable, &error2); - if (rels == NULL) { - if (g_error_matches (error2,FWUPD_ERROR,FWUPD_ERROR_NOTHING_TO_DO)){ + if (!rels) + { + if (g_error_matches (error2,FWUPD_ERROR,FWUPD_ERROR_NOTHING_TO_DO)) + { qWarning() << "No Packages Found for "<< fwupd_device_get_id(device); FwupdHandleError(&error2); continue; } } - else{ + else + { fwupd_device_add_release(device,(FwupdRelease *)g_ptr_array_index(rels,0)); if(FwupdAddToSchedule(device)) { qWarning() << "Cannot Add To Schdule" << fwupd_device_get_id(device); continue; } } } } } -gchar* FwupdBackend::FwupdGetChecksum(const gchar *filename,GChecksumType checksum_type) +QByteArray FwupdBackend::FwupdGetChecksum(const QUrl filename,QCryptographicHash::Algorithm hashAlgorithm) { - gsize len; - g_autofree gchar *data = NULL; - if (!g_file_get_contents (filename, &data, &len, NULL)) { - qWarning() << "Cannot Access File!" << filename; - return NULL; + QFile f(filename.toString()); + if (f.open(QFile::ReadOnly)) + { + QCryptographicHash hash(hashAlgorithm); + if (hash.addData(&f)) + { + return hash.result().toHex(); + } } - return g_compute_checksum_for_data (checksum_type, (const guchar *)data, len); + return QByteArray(); } bool FwupdBackend::FwupdAddToSchedule(FwupdDevice *device) { FwupdRelease *rel = fwupd_device_get_release_default (device); GPtrArray *checksums; - const gchar *update_uri; - g_autofree gchar *basename = NULL; - g_autofree gchar *filename_cache = NULL; - g_autoptr(GFile) file = NULL; FwupdResource* app = NULL; /* update unsupported */ app = FwupdCreateRelease(device); if (!app->isLiveUpdatable) { qWarning() << app->m_name << "[" << app->m_id << "]" << "cannot be updated "; return false; } /* Important Attributes missing */ if (app->m_id.isNull()) { qWarning() << "fwupd: No id for firmware"; return true; } if (app->m_version.isNull()) { qWarning() << "fwupd: No version! for " << app->m_id; return true; } if (app->m_updateVersion.isNull()) { qWarning() << "fwupd: No update-version! for " << app->m_id; return true; } checksums = fwupd_release_get_checksums (rel); if (checksums->len == 0) { qWarning() << app->m_name << "[" << app->m_id << "]" << "(" << app->m_updateVersion << ")" "has no checksums, ignoring as unsafe"; return false; } - update_uri = fwupd_release_get_uri (rel); - if (update_uri == NULL) + const QUrl update_uri(QLatin1String(fwupd_release_get_uri(rel))); + + if (!update_uri.isValid()) { qWarning() << "no location available for" << app->m_name << "[" << app->m_id << "]"; return false; } /* Checking for firmware in the cache? */ - basename = g_path_get_basename (update_uri); - filename_cache = FwupdCacheFile("fwupd",basename); - if (filename_cache == NULL) + QFileInfo basename = QFileInfo(update_uri.path()); + const QUrl filename_cache = FwupdCacheFile(QStringLiteral("fwupd"),basename); + if (!filename_cache.isValid()) return false; - if (g_file_test (filename_cache, G_FILE_TEST_EXISTS)) + if (filename_cache.isLocalFile()) { - const gchar *checksum_tmp = NULL; - g_autofree gchar *checksum = NULL; + QByteArray checksum_tmp = QByteArray(fwupd_checksum_get_by_kind (checksums,G_CHECKSUM_SHA1)); /* Currently LVFS supports SHA1 only*/ - checksum_tmp = fwupd_checksum_get_by_kind (checksums,G_CHECKSUM_SHA1); - if (checksum_tmp == NULL) + if (checksum_tmp.isNull()) { qWarning() << "No valid checksum for" << filename_cache; } - checksum = FwupdGetChecksum(filename_cache,G_CHECKSUM_SHA1); - if (checksum == NULL) + QByteArray checksum = FwupdGetChecksum(filename_cache,QCryptographicHash::Sha1); + if (checksum.isNull()) return false; - if (g_strcmp0 (checksum_tmp, checksum) != 0) + if (checksum_tmp != checksum) { qWarning() << filename_cache << " does not match checksum, expected" << checksum_tmp << "got" << checksum; - g_unlink (filename_cache); + QFile::remove((filename_cache.toString())); return false; } } /* link file to application and append to update list */ - file = g_file_new_for_path (filename_cache); - app->m_file = file; - m_toUpdate.append(app); + QFile file(filename_cache.toString()); + app->m_file = &file; + if(!m_toUpdate.contains(app)) + m_toUpdate.append(app); /* schedule for download */ - if (!g_file_test (filename_cache, G_FILE_TEST_EXISTS)) + if (!filename_cache.isLocalFile()) FwupdAddToScheduleForDownload(update_uri); return true; } - - -bool FwupdBackend::FwupdDownloadFile(const gchar *uri,const gchar *filename) +bool FwupdBackend::FwupdDownloadFile(const QUrl &uri,const QString &filename) { - guint status_code; - g_autoptr(SoupMessage) msg = NULL; - g_return_val_if_fail (uri != NULL, FALSE); - g_return_val_if_fail (filename != NULL, FALSE); - g_autoptr(GCancellable) cancellable = g_cancellable_new(); - g_autoptr(GError) error_local = NULL; - - if (g_str_has_prefix (uri, "file://")) + QEventLoop loop; + QTimer getTimer; + QString fileName = filename; + QNetworkAccessManager *manager = new QNetworkAccessManager(this); + QTimer::connect(&getTimer,SIGNAL(timeout()),&loop, SLOT(quit())); + connect(manager, SIGNAL(finished(QNetworkReply*)),&loop, SLOT(quit())); + QNetworkReply *reply = manager->get(QNetworkRequest(uri)); + getTimer.start(600000); // 60 Seconds TimeOout Period + loop.exec(); + if(!reply) { - gsize length = 0; - g_autofree gchar *contents = NULL; - if (!g_file_get_contents (uri + 7, &contents, &length, &error_local)) - { - qWarning() << "Cannot Access File" << uri; - return false; - } - if (!g_file_set_contents (filename, contents, length, &error_local)) - { - qWarning() << "Cannot Save the file content in " << filename; - return false; - } - return true; - } - - /* remote */ - #ifdef FWUPD_DEBUG - qDebug() << "downloading " << uri << " to " << filename; - #endif - msg = soup_message_new (SOUP_METHOD_GET, uri); - if (msg == NULL) - { - // To DO Error Handling return false; } - - status_code = soup_session_send_message (soupSession, msg); - if (status_code != SOUP_STATUS_OK) + else if( QNetworkReply::NoError != reply->error() ) { - g_autoptr(GString) str = g_string_new (NULL); - g_string_append (str, soup_status_get_phrase (status_code)); - if (msg->response_body->data != NULL) - { - g_string_append (str, ": "); - g_string_append (str, msg->response_body->data); - } - // To DO Error Handling return false; } - - if (!g_file_set_contents (filename,msg->response_body->data,msg->response_body->length,&error_local)) + else { - qWarning() << "Cannot Save the file content in " << filename; - return false; - } - return true; -} - -GBytes* FwupdBackend::FwupdDownloadData(const gchar *uri) -{ - guint status_code; - g_autoptr(SoupMessage) msg = NULL; - g_autoptr(GCancellable) cancellable = g_cancellable_new(); - g_autoptr(GError) error = NULL; - - g_return_val_if_fail (uri != NULL, NULL); - - /* local */ - if (g_str_has_prefix (uri, "file://")) { - gsize length = 0; - g_autofree gchar *contents = NULL; - g_autoptr(GError) error_local = NULL; - - if (!g_file_get_contents (uri + 7, &contents, &length, &error_local)) { - // To DO Error Handling - return NULL; - } - return g_bytes_new (contents, length); - } - - /* remote */ - msg = soup_message_new (SOUP_METHOD_GET, uri); - status_code = soup_session_send_message (soupSession, msg); - if (status_code != SOUP_STATUS_OK) { - g_autoptr(GString) str = g_string_new (NULL); - g_string_append (str, soup_status_get_phrase (status_code)); - if (msg->response_body->data != NULL) { - g_string_append (str, ": "); - g_string_append (str, msg->response_body->data); + QByteArray Data = reply->readAll(); + QFile file(fileName); + if (file.open(QIODevice::WriteOnly)) + { + file.write(Data); } - // TO Do Error Handling - return NULL; + file.close(); + delete reply; + delete manager; + return true; } - return g_bytes_new (msg->response_body->data,(gsize) msg->response_body->length); } -bool FwupdBackend::FwupdRefreshRemotes(guint cache_age) +bool FwupdBackend::FwupdRefreshRemotes(uint cacheAge) { - g_autoptr(GPtrArray) remotes = NULL; g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; - - /*Gets all Remotes will filter later*/ - remotes = fwupd_client_get_remotes (client, cancellable, &error); - if (remotes == NULL) + g_autoptr(GPtrArray) remotes = fwupd_client_get_remotes (client, cancellable, &error); + if (!remotes) return false; - for (int i = 0; i < (int)remotes->len; i++) { + for (uint i = 0; i < remotes->len; i++) + { FwupdRemote *remote = (FwupdRemote *)g_ptr_array_index (remotes, i); /*Remotes disabled by user so ignore*/ if (!fwupd_remote_get_enabled (remote)) continue; /*Local Remotes Ignore*/ if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) continue; /*Refresh the left ones*/ - if (!FwupdRefreshRemote(remote, cache_age)) + if (!FwupdRefreshRemote(remote, cacheAge)) return false; } return true; } -bool FwupdBackend::FwupdRefreshRemote(FwupdRemote *remote,guint cache_age) +bool FwupdBackend::FwupdRefreshRemote(FwupdRemote* remote,uint cacheAge) { - GChecksumType checksum_kind; - const gchar *url_sig = NULL; - const gchar *url = NULL; - g_autoptr(GError) error_local = NULL; - g_autofree gchar *basename = NULL; - g_autofree gchar *basename_sig = NULL; - g_autofree gchar *cache_id = NULL; - g_autofree gchar *checksum = NULL; - g_autofree gchar *filename = NULL; - g_autofree gchar *filename_sig = NULL; - g_autoptr(GBytes) data = NULL; g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; - if (fwupd_remote_get_filename_cache_sig (remote) == NULL) { + if (fwupd_remote_get_filename_cache_sig (remote) == NULL) + { qWarning() << "Remote " << fwupd_remote_get_id (remote) << "has no cache signature!"; return false; } /* check cache age */ - if (cache_age > 0) + if (cacheAge > 0) { - guint64 age = fwupd_remote_get_age (remote); - guint tmp = age < G_MAXUINT ? (guint) age : G_MAXUINT; - if (tmp < cache_age) + quint64 age = fwupd_remote_get_age (remote); + uint tmp = age < std::numeric_limits::max() ? (uint) age : std::numeric_limits::max(); + if (tmp < cacheAge) { #ifdef FWUPD_DEBUG - qDebug() << filename_sig << "is only" << tmp << "seconds old, so ignoring refresh! "; + qDebug() << remote << "is only" << tmp << "seconds old, so ignoring refresh! "; #endif return true; } } - cache_id = g_strdup_printf ("fwupd/remotes.d/%s", fwupd_remote_get_id (remote)); - basename_sig = g_path_get_basename (fwupd_remote_get_filename_cache_sig (remote)); - filename_sig = FwupdCacheFile(cache_id,basename_sig); - + QString cacheId = QStringLiteral("fwupd/remotes.d/%1").arg(QString::fromUtf8(fwupd_remote_get_id (remote))); + QFileInfo basenameSig = QFileInfo(QString::fromUtf8(g_path_get_basename(fwupd_remote_get_filename_cache_sig (remote)))); + const QUrl filename_sig = FwupdCacheFile(cacheId,basenameSig); + + if (filename_sig.isEmpty()) + return false; + /* download the signature first*/ - url_sig = fwupd_remote_get_metadata_uri_sig (remote); + const QUrl url_sig(QLatin1String(fwupd_remote_get_metadata_uri_sig(remote))); #ifdef FWUPD_DEBUG - qDebug() << "Download Remotes Signature"; + qDebug() << "downloading remotes signatures ..."; #endif - data = FwupdDownloadData(url_sig); + + const QUrl filename_sig_(filename_sig.toString() + QStringLiteral(".tmp")); - if (data == NULL) { - //To Do Error Handling + if(!FwupdDownloadFile(url_sig,filename_sig_.toString())) + { + qDebug() << "remote signature download failed ..."; return false; } - checksum_kind = fwupd_checksum_guess_kind (fwupd_remote_get_checksum (remote)); - checksum = g_compute_checksum_for_data (checksum_kind,(const guchar *) g_bytes_get_data (data, NULL),g_bytes_get_size (data)); - - if (g_strcmp0 (checksum, fwupd_remote_get_checksum (remote)) == 0) + QMap map = initHashMap(); + QCryptographicHash::Algorithm hashAlgorithm = map[(fwupd_checksum_guess_kind (fwupd_remote_get_checksum (remote)))]; + QByteArray hash = FwupdGetChecksum(filename_sig_,hashAlgorithm); + + if (fwupd_remote_get_checksum (remote) == hash) { #ifdef FWUPD_DEBUG - qDebug() << "signature of" << url_sig << "is unchanged"; + qDebug() << "signature of" << url_sig.toString() << "is unchanged"; #endif return true; } - + /* save to a file */ #ifdef FWUPD_DEBUG - qDebug() << "saving new remote signature to:" << filename_sig; + qDebug() << "saving new remote signature to:" << filename_sig.toString(); #endif - if (!g_file_set_contents (filename_sig,(const gchar*)g_bytes_get_data (data, NULL),(guint) g_bytes_get_size (data),&error_local)) + + if (!(QFile::copy(filename_sig_.toString(),filename_sig.toString()))) { - qWarning() << "cannot save signature"; + qWarning() << "cannot save remote signature"; return false; } + else + { + QFile::remove(filename_sig_.toString()); + } - basename = g_path_get_basename (fwupd_remote_get_filename_cache (remote)); - filename = FwupdCacheFile(cache_id,basename); - - if (filename == NULL) - return FALSE; + QFileInfo basename = QFileInfo(QString::fromUtf8(g_path_get_basename (fwupd_remote_get_filename_cache (remote)))); + const QUrl filename = FwupdCacheFile(cacheId,basename); + + if (filename.isEmpty()) + return false; + #ifdef FWUPD_DEBUG qDebug() << "saving new firmware metadata to:" << filename; - qDebug() << "Downloading remotes metadata"; #endif - url = fwupd_remote_get_metadata_uri (remote); - if (!FwupdDownloadFile (url, filename)) + + const QUrl url(QLatin1String(fwupd_remote_get_metadata_uri (remote))); + if (!FwupdDownloadFile (url, filename.toString())) { - qWarning() << "Cannot Download File : " << filename ; + qWarning() << "cannot download file : " << filename ; return false; } - /* Sending Metadata to fwupd Daemon*/ - if (!fwupd_client_update_metadata (client,fwupd_remote_get_id (remote),filename,filename_sig,cancellable,&error)) + if (!fwupd_client_update_metadata (client,fwupd_remote_get_id (remote),filename.toString().toUtf8().constData(),filename_sig.toString().toUtf8().constData(),cancellable,&error)) { FwupdHandleError(&error); return false; } - return true; } void FwupdBackend::FwupdHandleError(GError **perror) { GError *error = perror != NULL ? *perror : NULL; - if(error == NULL) + if(!error) return; //To DO Indivitual take action based on case,Show Notification on Discover; switch (error->code) { case FWUPD_ERROR_ALREADY_PENDING: qWarning() << "FWUPD_ERROR_ALREADY_PENDING"; break; case FWUPD_ERROR_INVALID_FILE: qWarning() << "FWUPD_ERROR_INVALID_FILE"; break; case FWUPD_ERROR_NOT_SUPPORTED: qWarning() << "FWUPD_ERROR_NOT_SUPPORTED"; break; case FWUPD_ERROR_AUTH_FAILED: qWarning() << "FWUPD_ERROR_AUTH_FAILED"; break; case FWUPD_ERROR_SIGNATURE_INVALID: qWarning() << "FWUPD_ERROR_SIGNATURE_INVALID"; break; case FWUPD_ERROR_AC_POWER_REQUIRED: qWarning() << "FWUPD_ERROR_AC_POWER_REQUIRED"; break; default: qWarning() << "Unknown Error ::" << error->code; break; } } -gchar* FwupdBackend::FwupdCacheFile(const gchar *kind,const gchar *resource) +const QUrl FwupdBackend::FwupdCacheFile(const QString &kind,const QFileInfo &resource) { - g_autofree gchar *basename = NULL; - g_autofree gchar *cachedir = NULL; - g_autoptr(GFile) cachedir_file = NULL; - g_autoptr(GPtrArray) candidates = g_ptr_array_new_with_free_func (g_free); - g_autoptr(GError) error = NULL; - basename = g_path_get_basename (resource); - cachedir = g_build_filename (g_get_user_cache_dir (),"discover",kind,NULL); - cachedir_file = g_file_new_for_path (cachedir); - - if (!g_file_query_exists (cachedir_file, NULL) && !g_file_make_directory_with_parents (cachedir_file, NULL, &error)) - return NULL; - g_ptr_array_add (candidates, g_build_filename (cachedir, basename, NULL)); - - return g_strdup((gchar *)g_ptr_array_index (candidates, 0)); + QString basename = resource.fileName(); + const QDir cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation)); + const QUrl cacheDirFile = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') + kind); + const QUrl fileUrl = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') + kind + QLatin1Char('/') + basename); + + if(!QFileInfo::exists(cacheDirFile.toLocalFile()) && !cacheDir.mkpath(kind)) + { + #ifdef FWUPD_DEBUG + qDebug() << "cannot make cache directory!"; + #endif + return QUrl(); + } + return QUrl(fileUrl.toString().remove(QStringLiteral("file://"))); } -bool FwupdBackend::FwupdDownloadAllScheduled(guint cache_age) +bool FwupdBackend::FwupdDownloadAllScheduled(uint cacheAge) { - const gchar *tmp; - if (!FwupdRefreshRemotes(cache_age)) + if (!FwupdRefreshRemotes(cacheAge)) return false; /* download the files to the cachedir */ - for (int i = 0; i < (int)toDownload->len; i++) + foreach(QUrl uri, toDownload) { - g_autoptr(GError) error_local = NULL; - g_autofree gchar *basename = NULL; - g_autofree gchar *filename_cache = NULL; - - tmp = (gchar *)g_ptr_array_index (toDownload, i); - basename = g_path_get_basename (tmp); - filename_cache = FwupdCacheFile("fwupd", basename); - - if (filename_cache == NULL) + const QUrl filename_cache = FwupdCacheFile(QStringLiteral("fwupd"),QFileInfo(uri.fileName())); + if(!filename_cache.isValid()) return false; - /* download file */ - if (!FwupdDownloadFile(tmp, filename_cache)) + if(!FwupdDownloadFile(uri,filename_cache.toString())) { - qWarning() <<"Failed to download " << tmp << ", ignoring:" ; - g_ptr_array_remove_index (toDownload, i--); - g_ptr_array_add (toIgnore, g_strdup (tmp)); - continue; + qWarning() <<"Failed to download " << uri.path() << "so, ignoring:" ; + if(!toIgnore.contains(uri)) + toIgnore.append(uri); } - } + } return true; } -bool FwupdBackend::FwupdAddToScheduleForDownload(const gchar * uri) +bool FwupdBackend::FwupdAddToScheduleForDownload(const QUrl uri) { - const gchar *tmp; - for (int i = 0; i < (int)toIgnore->len; i++) - { - tmp = (gchar *)g_ptr_array_index (toIgnore, i); - if (g_strcmp0 (tmp, uri) == 0) - return false; - } - for (int i = 0; i < (int)toDownload->len; i++) + if(toIgnore.contains(uri)) + return false; + if(toDownload.contains(uri)) + return false; + else { - tmp = (gchar *)g_ptr_array_index (toDownload, i); - if (g_strcmp0 (tmp, uri) == 0) - return false; + toDownload.append(uri); + return true; } - g_ptr_array_add (toDownload, g_strdup (uri)); - return true; } void FwupdBackend::toggleFetching() { m_fetching = !m_fetching; #ifdef FWUPD_DEBUG qDebug() << "Fwupd fetching..." << m_fetching; #endif FwupdAddUpdates(); - FwupdDownloadAllScheduled(60*60*24*30); // Nicer Way to put time? currently 30 days in seconds + FwupdDownloadAllScheduled(60*60*24*30); // Nicer Way to put time? currently 30 days in seconds emit fetchingChanged(); } int FwupdBackend::updatesCount() const { return m_updater->updatesCount(); } ResultsStream* FwupdBackend::search(const AbstractResourcesBackend::Filters& filter) { QVector ret; if (!filter.resourceUrl.isEmpty() && filter.resourceUrl.scheme() == QLatin1String("fwupd")) return findResourceByPackageName(filter.resourceUrl); else foreach(AbstractResource* r, m_resources) { if(r->name().contains(filter.search, Qt::CaseInsensitive) || r->comment().contains(filter.search, Qt::CaseInsensitive)) ret += r; } return new ResultsStream(QStringLiteral("Firmware Updates Stream"), ret); } ResultsStream * FwupdBackend::findResourceByPackageName(const QUrl& search) { auto res = search.scheme() == QLatin1String("fwupd") ? m_resources.value(search.host().replace(QLatin1Char('.'), QLatin1Char(' '))) : NULL; if (!res) { return new ResultsStream(QStringLiteral("Firmware Updates Stream"), {}); } else return new ResultsStream(QStringLiteral("Firmware Updates Stream"), { res }); } AbstractBackendUpdater* FwupdBackend::backendUpdater() const { return m_updater; } AbstractReviewsBackend* FwupdBackend::reviewsBackend() const { - //return m_reviews; // To Remove the Review backend ( not needed) return NULL; } Transaction* FwupdBackend::installApplication(AbstractResource* app, const AddonList& addons) { return new FwupdTransaction(qobject_cast(app), this, addons, Transaction::InstallRole); } Transaction* FwupdBackend::installApplication(AbstractResource* app) { return new FwupdTransaction(qobject_cast(app), this, Transaction::InstallRole); } Transaction* FwupdBackend::removeApplication(AbstractResource* app) { return new FwupdTransaction(qobject_cast(app), this, Transaction::RemoveRole); } void FwupdBackend::checkForUpdates() { if(m_fetching) return; toggleFetching(); populate(QStringLiteral("Releases")); QTimer::singleShot(500, this, &FwupdBackend::toggleFetching); #ifdef FWUPD_DEBUG qDebug() << "FwupdBackend::checkForUpdates"; #endif } AbstractResource * FwupdBackend::resourceForFile(const QUrl& path) { - g_autofree gchar *content_type = NULL; - g_autofree gchar *filename = NULL; - g_autoptr(GPtrArray) devices = NULL; g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; QMimeDatabase db; QMimeType type = db.mimeTypeForFile(path.fileName()); - + FwupdResource* app = NULL; + if(type.isValid() && type.inherits(QStringLiteral("application/vnd.ms-cab-compressed"))) { - filename = path.fileName().toUtf8().data(); - devices = fwupd_client_get_details (client,filename,cancellable,&error); + g_autofree gchar *filename = path.fileName().toUtf8().data(); + g_autoptr(GPtrArray) devices = fwupd_client_get_details (client,filename,cancellable,&error); - if (devices != NULL) + if (devices) { - for (int i = 0; i < (int)devices->len; i++) + FwupdDevice *dev = (FwupdDevice *)g_ptr_array_index (devices, 0); + app = FwupdCreateRelease(dev); + app->setState(AbstractResource::None); + for (uint i = 1; i < devices->len; i++) { FwupdDevice *dev = (FwupdDevice *)g_ptr_array_index (devices, i); - FwupdResource* app = NULL; - app = FwupdCreateRelease(dev); - app->setState(AbstractResource::None); - m_resources.insert(app->packageName(), app); - connect(app, &FwupdResource::stateChanged, this, &FwupdBackend::updatesCountChanged); - return app; + FwupdResource* app_ = FwupdCreateRelease(dev); + app_->setState(AbstractResource::None); + if(!app->m_releases.contains(app_)) + app->m_releases.append(app_); } + m_resources.insert(app->packageName(), app); + if(!m_toUpdate.contains(app)) + m_toUpdate.append(app); + connect(app, &FwupdResource::stateChanged, this, &FwupdBackend::updatesCountChanged); } else { FwupdHandleError(&error); } } - return NULL; + return app; } QString FwupdBackend::displayName() const { return QStringLiteral("Firmware Updates"); } bool FwupdBackend::hasApplications() const { return m_resources.count() ? true : false; } #include "FwupdBackend.moc" diff --git a/libdiscover/backends/FwupdBackend/FwupdBackend.h b/libdiscover/backends/FwupdBackend/FwupdBackend.h index f7ce3be3..5c1841fe 100644 --- a/libdiscover/backends/FwupdBackend/FwupdBackend.h +++ b/libdiscover/backends/FwupdBackend/FwupdBackend.h @@ -1,116 +1,116 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #ifndef FWUPDBACKEND_H #define FWUPDBACKEND_H #include + +#include +#include +#include +#include +#include +#include +#include #include #include +#include +#include +#include +#include +#include +#include +#include + extern "C" { #include } -#include #include -#include -#include -#include - - -#include -#include -#include #define FWUPD_DEBUG // UnComment This to see all debug messages class QAction; -class FwupdReviewsBackend; class FwupdUpdater; class FwupdResource; class FwupdBackend : public AbstractResourcesBackend { Q_OBJECT Q_PROPERTY(int startElements MEMBER m_startElements) public: explicit FwupdBackend(QObject* parent = NULL); ~FwupdBackend(); int updatesCount() const override; AbstractBackendUpdater* backendUpdater() const override; AbstractReviewsBackend* reviewsBackend() const override; ResultsStream* search(const AbstractResourcesBackend::Filters & search) override; ResultsStream * findResourceByPackageName(const QUrl& search) ; QHash resources() const { return m_resources; } bool isValid() const override { return true; } // No external file dependencies that could cause runtime errors Transaction* installApplication(AbstractResource* app) override; Transaction* installApplication(AbstractResource* app, const AddonList& addons) override; Transaction* removeApplication(AbstractResource* app) override; bool isFetching() const override { return m_fetching; } AbstractResource * resourceForFile(const QUrl & ) override; void checkForUpdates() override; QString displayName() const override; bool hasApplications() const override; FwupdClient *client; - GPtrArray *toDownload; - GPtrArray *toIgnore; + QList toDownload; + QList toIgnore; - bool FwupdDownloadFile(const gchar *uri,const gchar *filename); - GBytes* FwupdDownloadData(const gchar *uri); - bool FwupdRefreshRemotes(guint cache_age); - bool FwupdRefreshRemote(FwupdRemote *remote,guint cache_age); - gchar* FwupdCacheFile(const gchar *kind,const gchar *resource); - bool FwupdDownloadAllScheduled(guint cache_age); - bool FwupdAddToScheduleForDownload(const gchar * uri); + bool FwupdDownloadFile(const QUrl &uri,const QString &filename); + bool FwupdRefreshRemotes(uint cacheAge); + bool FwupdRefreshRemote(FwupdRemote *remote,uint cacheAge); + const QUrl FwupdCacheFile(const QString &kind,const QFileInfo &resource); + bool FwupdDownloadAllScheduled(uint cacheAge); + bool FwupdAddToScheduleForDownload(const QUrl uri); FwupdResource * FwupdCreateDevice(FwupdDevice *device); FwupdResource * FwupdCreateRelease(FwupdDevice *device); bool FwupdAddToSchedule(FwupdDevice *device); - gchar* FwupdGetChecksum(const gchar *filename,GChecksumType checksum_type); - gchar* FwupdBuildDeviceID(FwupdDevice* device); + QByteArray FwupdGetChecksum(const QUrl filename,QCryptographicHash::Algorithm hashAlgorithm); + QString FwupdBuildDeviceID(FwupdDevice* device); void FwupdAddUpdates(); void FwupdSetReleaseDetails(FwupdResource *res,FwupdRelease *rel); void FwupdSetDeviceDetails(FwupdResource *res,FwupdDevice *device); void FwupdHandleError(GError **perror); QSet FwupdGetAllUpdates(); QString FwupdGetAppName(QString ID); + QMap initHashMap(); public Q_SLOTS: void toggleFetching(); private: void populate(const QString& name); QHash m_resources; FwupdUpdater* m_updater; - FwupdReviewsBackend* m_reviews; bool m_fetching; int m_startElements; QList m_toUpdate; - - g_autofree gchar *userAgent = NULL; - g_autoptr(SoupSession) soupSession = NULL; - - }; #endif // FWUPDBACKEND_H diff --git a/libdiscover/backends/FwupdBackend/FwupdResource.cpp b/libdiscover/backends/FwupdBackend/FwupdResource.cpp index 7e6a6872..6ff4b3c7 100644 --- a/libdiscover/backends/FwupdBackend/FwupdResource.cpp +++ b/libdiscover/backends/FwupdBackend/FwupdResource.cpp @@ -1,186 +1,194 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #include "FwupdResource.h" #include #include #include #include FwupdResource::FwupdResource(QString name, bool isTechnical, AbstractResourcesBackend* parent) : AbstractResource(parent) , m_name(std::move(name)) , m_state(State::Broken) , m_isTechnical(isTechnical) { } QList FwupdResource::addonsInformation() { return m_addons; } QString FwupdResource::availableVersion() const { return m_version; } QStringList FwupdResource::allResourceNames() const { return { m_name }; } QStringList FwupdResource::categories() { return m_categories; } void FwupdResource::addCategories(const QString &category){ m_categories.append(category); } QString FwupdResource::comment() { return m_summary; } int FwupdResource::size() { return m_size; } QUrl FwupdResource::homepage() { return m_homepage; } QUrl FwupdResource::helpURL() { return m_homepage; } QUrl FwupdResource::bugURL() { return m_homepage; } QUrl FwupdResource::donationURL() { return m_homepage; } QVariant FwupdResource::icon() const { return m_iconName; } QString FwupdResource::installedVersion() const { return m_version; } QString FwupdResource::license() { return m_license; } QString FwupdResource::longDescription() { return m_description; } QString FwupdResource::name() const { return m_name; } QString FwupdResource::vendor() const { return m_vendor; } QString FwupdResource::origin() const { return m_homepage.toString(); } QString FwupdResource::packageName() const { return m_name; } QString FwupdResource::section() { return QStringLiteral("Firmware Updates"); } AbstractResource::State FwupdResource::state() { return m_state; } void FwupdResource::fetchChangelog() { QString log = longDescription(); log.replace(QLatin1Char('\n'), QLatin1String("
")); emit changelogFetched(log); } void FwupdResource::setState(AbstractResource::State state) { - m_state = state; - emit stateChanged(); + if(m_state != state) + { + m_state = state; + emit stateChanged(); + } + } void FwupdResource::setAddons(const AddonList& addons) { - Q_FOREACH (const QString& toInstall, addons.addonsToInstall()) { + Q_FOREACH (const QString& toInstall, addons.addonsToInstall()) + { setAddonInstalled(toInstall, true); } - Q_FOREACH (const QString& toRemove, addons.addonsToRemove()) { + Q_FOREACH (const QString& toRemove, addons.addonsToRemove()) + { setAddonInstalled(toRemove, false); } } void FwupdResource::setAddonInstalled(const QString& addon, bool installed) { - for(auto & elem : m_addons) { - if(elem.name() == addon) { + for(auto & elem : m_addons) + { + if(elem.name() == addon) + { elem.setInstalled(installed); } } } void FwupdResource::invokeApplication() const { QDesktopServices d; d.openUrl(QUrl(QStringLiteral("https://projects.kde.org/projects/extragear/sysadmin/muon"))); } QUrl FwupdResource::url() const { return m_homepage; } diff --git a/libdiscover/backends/FwupdBackend/FwupdResource.h b/libdiscover/backends/FwupdBackend/FwupdResource.h index b79b11b1..9935e2c3 100644 --- a/libdiscover/backends/FwupdBackend/FwupdResource.h +++ b/libdiscover/backends/FwupdBackend/FwupdResource.h @@ -1,117 +1,117 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #ifndef FWUPDRESOURCE_H #define FWUPDRESOURCE_H #include #include "FwupdBackend.h" class AddonList; class FwupdResource : public AbstractResource { Q_OBJECT public: explicit FwupdResource(QString name, bool isTechnical, AbstractResourcesBackend* parent); QList addonsInformation() override; QString section() override; QString origin() const override; QString longDescription() override; QString availableVersion() const override; QString installedVersion() const override; QString license() override; int size() override; QUrl homepage() override; QUrl helpURL() override; QUrl bugURL() override; QUrl donationURL() override; QStringList categories() override; AbstractResource::State state() override; QVariant icon() const override; QString comment() override; QString name() const override; QString packageName() const override; QString vendor() const; bool isTechnical() const override { return m_isTechnical; } bool canExecute() const override { return true; } void invokeApplication() const override; void fetchChangelog() override; QUrl url() const override; void setState(State state); void setSize(int size) { m_size = size; } void setAddons(const AddonList& addons); void setId(const QString &id){m_id = id;} void setName(const QString &name){ m_name = name;} void setSummary(const QString &summary){ m_summary = summary;} void setDescription(const QString &description){ m_description = description;} void setVersion(const QString &version){ m_version = version;} void setVendor(const QString &vendor){ m_vendor = vendor;} void addCategories(const QString &category); void setHomePage(const QUrl &homepage){ m_homepage = homepage;} void setLicense(const QString &license){ m_license = license;} void setIconName(const QString &iconName){ m_iconName = iconName;} virtual QStringList allResourceNames() const; void setIsDeviceLocked(bool status){ isDeviceLocked = status;} void setDeviceID(const QString &deviceID){ m_deviceID = deviceID;} void setUpdateURI(const QString &updateURI){m_updateURI = updateURI;} // void setFile(const QFile &file){m_file = file;}; void setAddonInstalled(const QString& addon, bool installed); QString sourceIcon() const override { return QStringLiteral("player-time"); } QDate releaseDate() const override { return {}; } public: QString m_id; QString m_name; QString m_summary; QString m_description; QString m_version; QString m_updateVersion; QString m_vendor; QStringList m_categories; QString m_license; AbstractResource::State m_state; QUrl m_homepage; QString m_iconName; QList m_addons; bool m_isTechnical; int m_size; bool isDeviceLocked = false; // True if device is locked! QString m_deviceID; QString m_updateURI; - GFile* m_file; + QFile* m_file; bool isOnlyOffline = false; // True if only offline updates bool isLiveUpdatable = false; // True if device is live updatable bool needsReboot = false; // True if device needs Reboot bool isDeviceRemoval = false; //True if device is Removal - gchar * guidString; + QString guidString; QList m_releases; // A list of all refrences to releases of a device. }; #endif // FWUPDRESOURCE_H diff --git a/libdiscover/backends/FwupdBackend/FwupdReviewsBackend.cpp b/libdiscover/backends/FwupdBackend/FwupdReviewsBackend.cpp deleted file mode 100644 index 32b2b4dd..00000000 --- a/libdiscover/backends/FwupdBackend/FwupdReviewsBackend.cpp +++ /dev/null @@ -1,82 +0,0 @@ -/*************************************************************************** - * Copyright © 2013 Aleix Pol Gonzalez * * - * * - * 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 2 of * - * the License or (at your option) version 3 or any later version * - * accepted by the membership of KDE e.V. (or its successor approved * - * by the membership of KDE e.V.), which shall act as a proxy * - * defined in Section 14 of version 3 of the license. * - * * - * 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 . * - ***************************************************************************/ - -#include "FwupdReviewsBackend.h" -#include "FwupdBackend.h" -#include "FwupdResource.h" -#include -#include -#include -#include -#include - -FwupdReviewsBackend::FwupdReviewsBackend(FwupdBackend* parent) - : AbstractReviewsBackend(parent) -{} - -void FwupdReviewsBackend::fetchReviews(AbstractResource* app, int page) -{ - if (page>=5) - return; - - QVector review; - for(int i=0; i<33; i++) { - review += ReviewPtr(new Review(app->name(), app->packageName(), QStringLiteral("en_US"), QStringLiteral("good morning"), QStringLiteral("the morning is very good"), QStringLiteral("fwupd"), - QDateTime(), true, page+i, i%5, 1, 1, app->packageName())); - } - emit reviewsReady(app, review, false); -} - -Rating* FwupdReviewsBackend::ratingForApplication(AbstractResource* app) const -{ - return m_ratings[app]; -} - -void FwupdReviewsBackend::initialize() -{ - int i = 11; - FwupdBackend* b = qobject_cast(parent()); - foreach(FwupdResource* app, b->resources()) { - if (m_ratings.contains(app)) - continue; - auto randomRating = qrand()%10; - Rating* rating = new Rating(app->packageName(), ++i, {{QStringLiteral("star5"), randomRating}}); - rating->setParent(this); - m_ratings.insert(app, rating); - app->ratingFetched(); - } - emit ratingsReady(); -} - -void FwupdReviewsBackend::submitUsefulness(Review* r, bool useful) -{ - qDebug() << "usefulness..." << r->applicationName() << r->reviewer() << useful; - r->setUsefulChoice(useful ? ReviewsModel::Yes : ReviewsModel::No); -} - -void FwupdReviewsBackend::submitReview(AbstractResource* res, const QString& a, const QString& b, const QString& c) -{ - qDebug() << "fwupd submit review" << res->name() << a << b << c; -} - -bool FwupdReviewsBackend::isResourceSupported(AbstractResource* /*res*/) const -{ - return true; -} diff --git a/libdiscover/backends/FwupdBackend/FwupdReviewsBackend.h b/libdiscover/backends/FwupdBackend/FwupdReviewsBackend.h deleted file mode 100644 index 671219e9..00000000 --- a/libdiscover/backends/FwupdBackend/FwupdReviewsBackend.h +++ /dev/null @@ -1,59 +0,0 @@ -/*************************************************************************** - * Copyright © 2013 Aleix Pol Gonzalez * - * * - * 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 2 of * - * the License or (at your option) version 3 or any later version * - * accepted by the membership of KDE e.V. (or its successor approved * - * by the membership of KDE e.V.), which shall act as a proxy * - * defined in Section 14 of version 3 of the license. * - * * - * 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 . * - ***************************************************************************/ - -#ifndef FWUPDREVIEWSBACKEND_H -#define FWUPDREVIEWSBACKEND_H - -#include -#include -#include - -class FwupdBackend; -class FwupdReviewsBackend : public AbstractReviewsBackend -{ -Q_OBJECT -public: - explicit FwupdReviewsBackend(FwupdBackend* parent = nullptr); - - QString userName() const override { return QStringLiteral("fwupd"); } - void login() override {} - void logout() override {} - void registerAndLogin() override {} - - Rating* ratingForApplication(AbstractResource* app) const override; - bool hasCredentials() const override { return false; } - void deleteReview(Review*) override {} - void fetchReviews(AbstractResource* app, int page = 1) override; - bool isFetching() const override { return false; } - void submitReview(AbstractResource*, const QString&, const QString&, const QString&) override; - void flagReview(Review*, const QString&, const QString&) override {} - void submitUsefulness(Review*, bool) override; - - void initialize(); - bool isResourceSupported(AbstractResource * res) const override; - -Q_SIGNALS: - void ratingsReady(); - -private: - QHash m_ratings; -}; - -#endif // FWUPDREVIEWSBACKEND_H diff --git a/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.cpp b/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.cpp index 1833c76a..ac9eeb8c 100644 --- a/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.cpp +++ b/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.cpp @@ -1,175 +1,161 @@ /*************************************************************************** * Copyright © 2014 Aleix Pol Gonzalez * * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #include "FwupdSourcesBackend.h" #include #include #include #include class FwupdSourcesModel : public QStandardItemModel { Q_OBJECT public: FwupdSourcesModel(FwupdSourcesBackend* backend) : QStandardItemModel(backend) , m_backend(backend) {} QHash roleNames() const override { auto roles = QStandardItemModel::roleNames(); roles[Qt::CheckStateRole] = "checked"; return roles; } bool setData(const QModelIndex & index, const QVariant & value, int role) override { auto item = itemFromIndex(index); if (!item) return false; remote = fwupd_client_get_remote_by_id(m_backend->backend->client,item->data(AbstractSourcesBackend::IdRole).toString().toUtf8().constData(),NULL,NULL); status = fwupd_remote_get_enabled(remote); switch(role) { case Qt::CheckStateRole: { if((value.toInt() == Qt::Checked) ) { m_backend->eulaRequired(QLatin1String(fwupd_remote_get_title(remote)),QLatin1String(fwupd_remote_get_agreement(remote))); connect(m_backend,&FwupdSourcesBackend::proceed,this, [=]() { - if((value.toInt() == Qt::Checked) ) - { if(fwupd_client_modify_remote(m_backend->backend->client,fwupd_remote_get_id(remote),QString(QLatin1String("Enabled")).toUtf8().constData(),(QString(QLatin1String("true")).toUtf8().constData()),NULL,NULL)) item->setData(value, role); - } - else - { - if(fwupd_client_modify_remote(m_backend->backend->client,fwupd_remote_get_id(remote),QString(QLatin1String("Enabled")).toUtf8().constData(),(QString(QLatin1String("false")).toUtf8().constData()),NULL,NULL)) - item->setData(value, role); - } + } + ); + connect(m_backend,&FwupdSourcesBackend::cancel,this, + [=]() + { + item->setCheckState(Qt::Unchecked); + Q_EMIT dataChanged(index,index,{}); + return false; } ); } else if(value.toInt() == Qt::Unchecked) { - if((value.toInt() == Qt::Checked) ) - { - if(fwupd_client_modify_remote(m_backend->backend->client,fwupd_remote_get_id(remote),QString(QLatin1String("Enabled")).toUtf8().constData(),(QString(QLatin1String("true")).toUtf8().constData()),NULL,NULL)) + if(fwupd_client_modify_remote(m_backend->backend->client,fwupd_remote_get_id(remote),QString(QLatin1String("Enabled")).toUtf8().constData(),(QString(QLatin1String("false")).toUtf8().constData()),NULL,NULL)) item->setData(value, role); - } - else - { - if(fwupd_client_modify_remote(m_backend->backend->client,fwupd_remote_get_id(remote),QString(QLatin1String("Enabled")).toUtf8().constData(),(QString(QLatin1String("false")).toUtf8().constData()),NULL,NULL)) - item->setData(value, role); - } } } } Q_EMIT dataChanged(index,index,{}); return true; } private: FwupdSourcesBackend* m_backend; FwupdRemote* remote; bool status; }; FwupdSourcesBackend::FwupdSourcesBackend(AbstractResourcesBackend * parent) : AbstractSourcesBackend(parent) , m_sources(new FwupdSourcesModel(this)) { backend = qobject_cast(parent); - g_autoptr(GPtrArray) remotes = NULL; g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; /* find all remotes */ - remotes = fwupd_client_get_remotes (backend->client,cancellable,&error); + g_autoptr(GPtrArray) remotes = fwupd_client_get_remotes (backend->client,cancellable,&error); if(remotes != NULL) { - for (int i = 0; i < (int)remotes->len; i++) + for (uint i = 0; i < remotes->len; i++) { FwupdRemote *remote = (FwupdRemote *)g_ptr_array_index (remotes, i); if (fwupd_remote_get_kind (remote) == FWUPD_REMOTE_KIND_LOCAL) continue; addSource(QLatin1String(fwupd_remote_get_id (remote))); } } } QAbstractItemModel* FwupdSourcesBackend::sources() { return m_sources; } void FwupdSourcesBackend::eulaRequired( const QString& remoteName , const QString& licenseAgreement) { Q_EMIT proceedRequest(i18n("Accept EULA"), i18n("The remote %1 require that you accept their license:\n %2", remoteName, licenseAgreement)); } -void FwupdSourcesBackend::cancel() -{ - qDebug() << "Request Cancelled"; -} - bool FwupdSourcesBackend::addSource(const QString& id) { g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; FwupdBackend* backend = qobject_cast(parent()); FwupdRemote* remote; bool status ; if (id.isEmpty()) return false; remote = fwupd_client_get_remote_by_id(backend->client,id.toUtf8().constData(),cancellable,&error); status = !fwupd_remote_get_enabled(remote); QStandardItem* it = new QStandardItem(id); it->setData(id, AbstractSourcesBackend::IdRole); it->setData(QVariant(QLatin1Literal(fwupd_remote_get_title (remote))), Qt::ToolTipRole); it->setCheckable(true); it->setCheckState(status ? Qt::Unchecked : Qt::Checked); m_sources->appendRow(it); return true; } bool FwupdSourcesBackend::removeSource(const QString& id) { qWarning() << "Removal of Sources Not Allowed" << "Remote-ID" << id; return false; } QList FwupdSourcesBackend::actions() const { return m_actions ; } #include "FwupdSourcesBackend.moc" diff --git a/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.h b/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.h index c4870762..2a60aa97 100644 --- a/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.h +++ b/libdiscover/backends/FwupdBackend/FwupdSourcesBackend.h @@ -1,56 +1,56 @@ /*************************************************************************** * Copyright © 2014 Aleix Pol Gonzalez * * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #ifndef FWUPDSOURCESBACKEND_H #define FWUPDSOURCESBACKEND_H #include #include "FwupdBackend.h" #include #include "FwupdBackend.h" class FwupdSourcesModel; class FwupdSourcesBackend : public AbstractSourcesBackend { Q_OBJECT public: explicit FwupdSourcesBackend(AbstractResourcesBackend * parent); FwupdBackend* backend ; QAbstractItemModel* sources() override; bool addSource(const QString& id) override; bool removeSource(const QString& id) override; QString idDescription() override { return QStringLiteral(""); } QList actions() const override; bool supportsAdding() const override { return false; } void eulaRequired(const QString& remoteName , const QString& licenseAgreement); - void cancel() override; Q_SIGNALS: void proceed() override; + void cancel() override; private: FwupdSourcesModel* m_sources; QList m_actions; }; #endif // FWUPDSOURCESBACKEND_H diff --git a/libdiscover/backends/FwupdBackend/FwupdTransaction.cpp b/libdiscover/backends/FwupdBackend/FwupdTransaction.cpp index 12f66ebc..e65604fc 100644 --- a/libdiscover/backends/FwupdBackend/FwupdTransaction.cpp +++ b/libdiscover/backends/FwupdBackend/FwupdTransaction.cpp @@ -1,191 +1,189 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * + * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #include "FwupdTransaction.h" #include #include #include #define TEST_PROCEED FwupdTransaction::FwupdTransaction(FwupdResource* app, FwupdBackend* backend, Role role) : FwupdTransaction(app, backend,{}, role) { } FwupdTransaction::FwupdTransaction(FwupdResource* app, FwupdBackend* backend, const AddonList& addons, Transaction::Role role) : Transaction(app->backend(), app, role, addons) , m_app(app) , m_backend(backend) { setCancellable(true); if(role == InstallRole) { if(!FwupdCheck()) qWarning() << "Error In Install!"; } else if(role == RemoveRole) { if(!FwupdRemove()) qWarning() << "Error in Remove!"; } iterateTransaction(); } FwupdTransaction::~FwupdTransaction() { } bool FwupdTransaction::FwupdCheck() { g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; if(m_app->isDeviceLocked) { - const gchar *device_id; - device_id = m_app->m_deviceID.toUtf8().constData(); - if(device_id == NULL) + QString device_id = m_app->m_deviceID; + if(device_id.isNull()) { qWarning("No Device ID Set"); return false; } - if (!fwupd_client_unlock (m_backend->client, device_id,cancellable, &error)) + if (!fwupd_client_unlock (m_backend->client, device_id.toUtf8().constData(),cancellable, &error)) { m_backend->FwupdHandleError(&error); return false; } return true; } if(!FwupdInstall()) { // To DO error handling return false; } return true; } bool FwupdTransaction::FwupdInstall() { - const gchar *device_id; FwupdInstallFlags install_flags = FWUPD_INSTALL_FLAG_NONE;//Removed 0 check for ussage - GFile *local_file; - g_autofree gchar *filename = NULL; g_autoptr(GCancellable) cancellable = g_cancellable_new(); g_autoptr(GError) error = NULL; - local_file = m_app->m_file; + QFile *local_file = m_app->m_file; - if(local_file == NULL) + if(!local_file) { //to Do error handling qWarning("No Local File Set For this Resource"); return false; } - filename = g_file_get_path (local_file); + QString filename = local_file->fileName(); - if (!g_file_query_exists (local_file, cancellable)) + if (!(QFileInfo::exists(filename) && QFileInfo(filename).isFile())) { - const gchar *uri = m_app->m_updateURI.toUtf8().constData(); + const QUrl uri(m_app->m_updateURI); if(!m_backend->FwupdDownloadFile(uri,filename)) return false; } /* limit to single device? */ - device_id = m_app->m_deviceID.toUtf8().constData(); - if (device_id == NULL) - device_id = FWUPD_DEVICE_ID_ANY; + QString device_id = m_app->m_deviceID; + if (device_id.isNull()) + device_id = QStringLiteral(FWUPD_DEVICE_ID_ANY); /* only offline supported */ if (m_app->isOnlyOffline) install_flags = FWUPD_INSTALL_FLAG_OFFLINE; // removed the bit wise or operation |= - if (!fwupd_client_install (m_backend->client, device_id,filename, install_flags,cancellable, &error)) { + if (!fwupd_client_install (m_backend->client, device_id.toUtf8().constData(),filename.toUtf8().constData(), install_flags,cancellable, &error)) { m_backend->FwupdHandleError(&error); return false; } return true; } bool FwupdTransaction::FwupdRemove() { // To Do Implement It return true; } int FwupdTransaction::speed() { //To Do Implement It return 0; } void FwupdTransaction::iterateTransaction() { if (!m_iterate) return; setStatus(CommittingStatus); - if(progress()<100) { + if(progress()<100) + { setProgress(fwupd_client_get_percentage (m_backend->client)); QTimer::singleShot(100, this, &FwupdTransaction::iterateTransaction); } else #ifdef TEST_PROCEED Q_EMIT proceedRequest(QStringLiteral("yadda yadda"), QStringLiteral("Biii BOooo
  • A
  • A
  • A
  • A
")); #else finishTransaction(); #endif } void FwupdTransaction::proceed() { finishTransaction(); } void FwupdTransaction::cancel() { m_iterate = false; setStatus(CancelledStatus); } void FwupdTransaction::finishTransaction() { AbstractResource::State newState; switch(role()) { case InstallRole: case ChangeAddonsRole: newState = AbstractResource::Installed; break; case RemoveRole: newState = AbstractResource::None; break; } m_app->setAddons(addons()); m_app->setState(newState); setStatus(DoneStatus); deleteLater(); } diff --git a/libdiscover/backends/FwupdBackend/FwupdTransaction.h b/libdiscover/backends/FwupdBackend/FwupdTransaction.h index 300bff22..0c7a54ad 100644 --- a/libdiscover/backends/FwupdBackend/FwupdTransaction.h +++ b/libdiscover/backends/FwupdBackend/FwupdTransaction.h @@ -1,54 +1,55 @@ /*************************************************************************** * Copyright © 2013 Aleix Pol Gonzalez * + * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #ifndef FWUPDTRANSACTION_H #define FWUPDTRANSACTION_H #include #include "FwupdBackend.h" #include "FwupdResource.h" class FwupdResource; class FwupdTransaction : public Transaction { Q_OBJECT public: FwupdTransaction(FwupdResource* app, FwupdBackend* backend, Role role); FwupdTransaction(FwupdResource* app, FwupdBackend* backend, const AddonList& list, Role role); ~FwupdTransaction(); bool FwupdCheck(); bool FwupdInstall(); bool FwupdRemove(); void cancel() override; void proceed() override; int speed(); private Q_SLOTS: void iterateTransaction(); void finishTransaction(); private: bool m_iterate = true; FwupdResource* m_app; FwupdBackend* m_backend; }; #endif // FWUPDTRANSACTION_H diff --git a/libdiscover/backends/FwupdBackend/FwupdUpdater.cpp b/libdiscover/backends/FwupdBackend/FwupdUpdater.cpp index 14c8d04c..633b8d46 100644 --- a/libdiscover/backends/FwupdBackend/FwupdUpdater.cpp +++ b/libdiscover/backends/FwupdBackend/FwupdUpdater.cpp @@ -1,173 +1,178 @@ /*************************************************************************** * Copyright © 2018 Abhijeet Sharma * * * * 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 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * 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 . * ***************************************************************************/ #include #include #include FwupdUpdater::FwupdUpdater(FwupdBackend * parent) : AbstractBackendUpdater(parent), m_transaction(nullptr), m_backend(parent), m_isCancelable(false), m_isProgressing(false), m_percentage(0), m_lastUpdate() { } FwupdUpdater::~FwupdUpdater() { } void FwupdUpdater::prepare() { Q_ASSERT(!m_transaction); m_toUpgrade = m_backend->FwupdGetAllUpdates(); m_allUpgradeable = m_toUpgrade; - qDebug() << "Inside The Updater Prepare Method"; } int FwupdUpdater::updatesCount() { return m_toUpgrade.count(); } void FwupdUpdater::start() { Q_ASSERT(!isProgressing()); } void FwupdUpdater::cancel() { if (m_transaction) m_transaction->cancel(); else setProgressing(false); } void FwupdUpdater::setProgressing(bool progressing) { - if (m_isProgressing != progressing) { + if (m_isProgressing != progressing) + { m_isProgressing = progressing; emit progressingChanged(m_isProgressing); } } quint64 FwupdUpdater::downloadSpeed() const { return m_transaction ? m_transaction->speed() : 0; } double FwupdUpdater::updateSize() const { double ret = 0.; QSet donePkgs; - for (AbstractResource * res : m_toUpgrade) { + for (AbstractResource * res : m_toUpgrade) + { FwupdResource * app = qobject_cast(res); QString pkgid = app->m_id; - if (!donePkgs.contains(pkgid)) { + if (!donePkgs.contains(pkgid)) + { donePkgs.insert(pkgid); ret += app->size(); } } return ret; } bool FwupdUpdater::isMarked(AbstractResource* res) const { return m_toUpgrade.contains(res); } bool FwupdUpdater::isProgressing() const { return m_isProgressing; } bool FwupdUpdater::isCancelable() const { return m_isCancelable; } QDateTime FwupdUpdater::lastUpdate() const { return m_lastUpdate; } QList FwupdUpdater::toUpdate() const { return m_toUpgrade.toList(); } void FwupdUpdater::addResources(const QList& apps) { QSet pkgs = involvedResources(apps.toSet()); m_toUpgrade.unite(resourcesForResourceId(pkgs)); } void FwupdUpdater::removeResources(const QList& apps) { QSet pkgs = involvedResources(apps.toSet()); m_toUpgrade.subtract(resourcesForResourceId(pkgs)); } QSet FwupdUpdater::involvedResources(const QSet& resources) const { QSet resIds; resIds.reserve(resources.size()); - foreach (AbstractResource * res, resources) { + foreach (AbstractResource * res, resources) + { FwupdResource * app = qobject_cast(res); QString resid = app->m_id; resIds.insert(resid); } return resIds; } QSet FwupdUpdater::resourcesForResourceId(const QSet& resids) const { QSet resources; resources.reserve(resids.size()); foreach(const QString& resid, resids) { resources += m_backend->FwupdGetAppName(resid); } QSet ret; - foreach (AbstractResource * res, m_allUpgradeable) { + foreach (AbstractResource * res, m_allUpgradeable) + { FwupdResource* pres = qobject_cast(res); - if (resources.contains(pres->allResourceNames().toSet())) { + if (resources.contains(pres->allResourceNames().toSet())) + { ret.insert(res); } } return ret; } qreal FwupdUpdater::progress() const { return m_percentage; } bool FwupdUpdater::hasUpdates() const { return m_backend->updatesCount() > 0; } diff --git a/libdiscover/backends/FwupdBackend/fwupd-backend-categories.xml b/libdiscover/backends/FwupdBackend/fwupd-backend-categories.xml deleted file mode 100644 index 58605af3..00000000 --- a/libdiscover/backends/FwupdBackend/fwupd-backend-categories.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - Firmware Updates - applications-internet - pkcategories/applicationcrop.jpg - - - Firmware Updates - - - - - Releases - pkcategories/officecrop.jpg - applications-office - - - Releases - - - - - - -