LCOV - code coverage report
Current view: top level - src/tools/qt-gui/src - guibackend.cpp (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 0 137 0.0 %
Date: 2019-09-12 12:28:41 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  */
       8             : 
       9             : #include "guibackend.hpp"
      10             : 
      11             : #include <QDebug>
      12             : #include <QRegExp>
      13             : #include <backendbuilder.hpp>
      14             : #include <backends.hpp>
      15             : #include <discountmarkdownconverter.h>
      16             : #include <markdowndocument.h>
      17             : #include <string>
      18             : #include <vector>
      19             : 
      20             : using namespace std;
      21             : using namespace kdb;
      22             : using namespace kdb::tools;
      23             : 
      24           0 : GUIBackend::GUIBackend (QObject * parentBackend) : QObject (parentBackend), m_backend (nullptr)
      25             : {
      26           0 :         m_pluginConfigModel = new TreeViewModel;
      27           0 :         resetModel ();
      28           0 : }
      29             : 
      30           0 : void GUIBackend::createBackend (const QString & mountpoint)
      31             : {
      32           0 :         m_backend = QSharedPointer<MountBackendBuilder> (new MountBackendBuilder ());
      33             : 
      34           0 :         Key parentKey (Backends::mountpointsPath, KEY_END);
      35             : 
      36             :         try
      37             :         {
      38           0 :                 m_kdb.get (m_mountConf, parentKey);
      39             :         }
      40           0 :         catch (KDBException const & ex)
      41             :         {
      42           0 :                 emit showMessage (tr ("Error"), tr ("Could not read from configuration."), QString (ex.what ()));
      43             :         }
      44             : 
      45             :         try
      46             :         {
      47           0 :                 m_backend->setMountpoint (Key (mountpoint.toStdString (), KEY_CASCADING_NAME, KEY_END), m_mountConf);
      48             :         }
      49           0 :         catch (MountpointInvalidException const & ex)
      50             :         {
      51           0 :                 emit showMessage (tr ("Error"), tr ("The provided mountpoint is invalid."), ex.what ());
      52             :                 return;
      53             :         }
      54           0 :         catch (MountpointAlreadyInUseException const & ex)
      55             :         {
      56           0 :                 emit showMessage (tr ("Error"), tr ("The provided mountpoint is one of the already used cascading names."), ex.what ());
      57             :                 return;
      58             :         }
      59             : }
      60             : 
      61           0 : void GUIBackend::addPath (const QString & path)
      62             : {
      63             :         try
      64             :         {
      65           0 :                 m_backend->useConfigFile (path.toStdString ());
      66             :         }
      67           0 :         catch (FileNotValidException const & ex)
      68             :         {
      69           0 :                 emit showMessage (tr ("Error"), tr ("The file you have entered is not valid."), ex.what ());
      70             :         }
      71           0 :         catch (MissingSymbol const & ex)
      72             :         {
      73           0 :                 emit showMessage (tr ("Error"), tr ("Could not add file."), ex.what ());
      74             :         }
      75           0 : }
      76             : 
      77           0 : void GUIBackend::addPlugin (QString plugin, bool recommended)
      78             : {
      79           0 :         plugin.chop (plugin.length () - plugin.indexOf ("[") + 1);
      80             : 
      81             :         try
      82             :         {
      83           0 :                 PluginSpec spec (plugin.toStdString (), m_pluginConfigModel->collectCurrentKeySet ().dup ());
      84           0 :                 m_backend->addPlugin (spec);
      85           0 :                 m_backend->resolveNeeds (recommended);
      86             :         }
      87           0 :         catch (PluginCheckException const & ex)
      88             :         {
      89           0 :                 emit showMessage (tr ("Error"), tr ("Could not add plugin \"%1\".").arg (plugin), ex.what ());
      90             :         }
      91             : 
      92           0 :         resetModel ();
      93           0 : }
      94             : 
      95           0 : void GUIBackend::removePlugin (QString plugin)
      96             : {
      97           0 :         if (plugin.indexOf ("[") > -1) plugin.chop (plugin.length () - plugin.indexOf ("[") + 1);
      98             : 
      99             :         try
     100             :         {
     101           0 :                 PluginSpec spec (plugin.toStdString (), m_pluginConfigModel->collectCurrentKeySet ().dup ());
     102           0 :                 m_backend->remPlugin (spec);
     103             :         }
     104           0 :         catch (PluginCheckException const & ex)
     105             :         {
     106           0 :                 emit showMessage (tr ("Error"), tr ("Could not remove plugin \"%1\".").arg (plugin), ex.what ());
     107             :         }
     108             : 
     109           0 :         resetModel ();
     110           0 : }
     111             : 
     112           0 : void GUIBackend::serialise (TreeViewModel * model)
     113             : {
     114             :         try
     115             :         {
     116           0 :                 m_backend->serialize (m_mountConf);
     117             :         }
     118           0 :         catch (ToolException const & ex)
     119             :         {
     120           0 :                 emit showMessage (tr ("Error"), tr ("Could not serialise backend."), ex.what ());
     121             :         }
     122             : 
     123           0 :         model->createNewNodes (m_mountConf);
     124             : 
     125             :         try
     126             :         {
     127           0 :                 Key rootKey (Backends::mountpointsPath, KEY_END);
     128           0 :                 m_kdb.get (m_mountConf, rootKey);
     129           0 :                 m_kdb.set (m_mountConf, rootKey);
     130             :         }
     131           0 :         catch (kdb::KDBException const & ex)
     132             :         {
     133           0 :                 emit showMessage (tr ("Error"), tr ("Could not write backend to configuration."), ex.what ());
     134             :         }
     135           0 : }
     136             : 
     137           0 : QStringList GUIBackend::addedPlugins () const
     138             : {
     139           0 :         QStringList pluginList;
     140             : 
     141           0 :         for (PluginSpec const & elem : *m_backend)
     142             :         {
     143           0 :                 pluginList.append (QString::fromStdString (elem.getName ()));
     144             :         }
     145             : 
     146           0 :         return pluginList;
     147             : }
     148             : 
     149           0 : bool GUIBackend::pluginAlreadyAdded (QString plugin) const
     150             : {
     151           0 :         if (plugin.indexOf ("[") > -1) plugin.chop (plugin.length () - plugin.indexOf ("[") + 1);
     152             : 
     153           0 :         for (PluginSpec const & elem : *m_backend)
     154             :         {
     155           0 :                 if (QString::fromStdString (elem.getName ()) == plugin) return true;
     156             :         }
     157             : 
     158             :         return false;
     159             : }
     160             : 
     161           0 : bool GUIBackend::validated ()
     162             : {
     163           0 :         return m_backend->validated ();
     164             : }
     165             : 
     166           0 : TreeViewModel * GUIBackend::pluginConfigModel () const
     167             : {
     168           0 :         return m_pluginConfigModel;
     169             : }
     170             : 
     171           0 : void GUIBackend::resetModel ()
     172             : {
     173           0 :         KeySet emptySet;
     174           0 :         m_pluginConfigModel->populateModel (emptySet);
     175           0 : }
     176             : 
     177           0 : QString GUIBackend::mountPoints () const
     178             : {
     179           0 :         Key parentKey (Backends::mountpointsPath, KEY_END);
     180           0 :         KeySet mountConf;
     181           0 :         KDB kdb (parentKey);
     182             : 
     183             :         try
     184             :         {
     185           0 :                 kdb.get (mountConf, parentKey);
     186             :         }
     187           0 :         catch (KDBException const & ex)
     188             :         {
     189           0 :                 emit showMessage (tr ("Error"), tr ("Could not read from configuration."), QString (ex.what ()));
     190           0 :                 return "";
     191             :         }
     192             : 
     193           0 :         Backends::BackendInfoVector vec = Backends::getBackendInfo (mountConf);
     194           0 :         QStringList mPoints;
     195           0 :         mPoints.append ("system/elektra");
     196             : 
     197           0 :         foreach (BackendInfo info, vec)
     198             :         {
     199           0 :                 QString backend = QString::fromStdString (info.name);
     200             : 
     201           0 :                 if (backend.startsWith ("/"))
     202             :                 {
     203           0 :                         mPoints.append ("dir" + backend);
     204           0 :                         mPoints.append ("user" + backend);
     205           0 :                         mPoints.append ("system" + backend);
     206             :                 }
     207             :                 else
     208             :                 {
     209           0 :                         mPoints.append (backend);
     210             :                 }
     211             :         }
     212             : 
     213           0 :         return mPoints.join (", ");
     214             : }
     215             : 
     216           0 : QString GUIBackend::pluginInfo (QString pluginName) const
     217             : {
     218           0 :         Modules modules;
     219           0 :         KeySet info;
     220           0 :         QString infoString;
     221             : 
     222           0 :         pluginName.chop (pluginName.length () - pluginName.indexOf ("[") + 1);
     223             : 
     224           0 :         PluginPtr plugin = modules.load (pluginName.toStdString ());
     225             : 
     226           0 :         info = plugin->getInfo ();
     227             : 
     228           0 :         Key root;
     229           0 :         root.setName (std::string ("system/elektra/modules/") + plugin->name () + "/infos");
     230           0 :         Key k = info.lookup (root);
     231             : 
     232           0 :         if (k)
     233             :         {
     234           0 :                 while ((k = info.next ()) && k.isBelow (root))
     235             :                 {
     236           0 :                         infoString.append (QString::fromStdString (k.getBaseName ()) + ": " + QString::fromStdString (k.getString ()) +
     237           0 :                                            "\n\n");
     238             :                 }
     239             :         }
     240             :         else
     241           0 :                 infoString.append (tr ("No information found."));
     242             : 
     243           0 :         DiscountMarkdownConverter dmc;
     244           0 :         infoString = dmc.renderAsHtml (dmc.createDocument (infoString, DiscountMarkdownConverter::NoImagesOption));
     245             : 
     246           0 :         return infoString;
     247             : }
     248             : 
     249           0 : QStringList GUIBackend::availablePlugins (bool includeStorage, bool includeResolver) const
     250             : {
     251           0 :         QStringList availPlugins;
     252           0 :         Modules modules;
     253           0 :         PluginPtr ptr;
     254           0 :         QString type;
     255             : 
     256           0 :         vector<string> pluginVector = ModulesPluginDatabase ().listAllPlugins ();
     257             : 
     258           0 :         foreach (string s, pluginVector)
     259             :         {
     260             :                 try
     261             :                 {
     262           0 :                         ptr = modules.load (s);
     263             :                 }
     264           0 :                 catch (NoPlugin const & ex)
     265             :                 {
     266             :                         continue;
     267             :                 }
     268             : 
     269           0 :                 ptr->loadInfo ();
     270           0 :                 type = pluginType (QString::fromStdString (s));
     271             : 
     272           0 :                 if (!((!includeStorage && type == "storage") || (!includeResolver && type == "resolver")))
     273             :                 {
     274           0 :                         availPlugins.append (QString::fromStdString (s) + QString::fromStdString (" [%1]").arg (type));
     275             :                 }
     276             :         }
     277             : 
     278           0 :         availPlugins.sort ();
     279             : 
     280           0 :         return availPlugins;
     281             : }
     282             : 
     283           0 : QString GUIBackend::pluginType (QString plugin) const
     284             : {
     285           0 :         Modules modules;
     286           0 :         PluginPtr ptr;
     287             : 
     288             :         try
     289             :         {
     290           0 :                 ptr = modules.load (plugin.toStdString ());
     291             :         }
     292           0 :         catch (NoPlugin const & ex)
     293             :         {
     294           0 :                 return "";
     295             :         }
     296             : 
     297           0 :         return QString::fromStdString (ptr->lookupInfo ("provides"));
     298             : }
     299             : 
     300           0 : QStringList GUIBackend::nameFilters ()
     301             : {
     302           0 :         QStringList nFilters;
     303           0 :         QStringList plugins = availablePlugins (true, false);
     304             : 
     305           0 :         plugins = plugins.filter ("[storage]");
     306           0 :         plugins.replaceInStrings (QRegExp ("\\s\\[\\w*\\]"), "");
     307             : 
     308           0 :         foreach (QString plugin, plugins)
     309             :         {
     310           0 :                 QString pattern = "*";
     311           0 :                 nFilters.append (QString ("%1 (%2)").arg (plugin, pattern));
     312             :         }
     313             : 
     314           0 :         nFilters.sort ();
     315             : 
     316           0 :         return nFilters;
     317           0 : }

Generated by: LCOV version 1.13