1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/* BEGIN_COMMON_COPYRIGHT_HEADER
* (c)LGPL2+
*
* LXQt - a lightweight, Qt based, desktop toolset
* https://lxqt.org
*
* Copyright: 2015 LXQt team
*
* This program or library is free software; you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* END_COMMON_COPYRIGHT_HEADER */
#include "panelpluginsmodel.h"
#include "plugin.h"
#include "ilxqtpanelplugin.h"
#include "lxqtpanel.h"
#include "lxqtpanelapplication.h"
#include <QPointer>
#include <XdgIcon>
#include <LXQt/Settings>
#include <QDebug>
PanelPluginsModel::PanelPluginsModel(LXQtPanel * panel,
QString const & namesKey,
QStringList const & desktopDirs,
QObject * parent/* = nullptr*/)
: QAbstractListModel{parent},
mNamesKey(namesKey),
mPanel(panel)
{
loadPlugins(desktopDirs);
}
PanelPluginsModel::~PanelPluginsModel()
{
qDeleteAll(plugins());
}
int PanelPluginsModel::rowCount(const QModelIndex & parent/* = QModelIndex()*/) const
{
return QModelIndex() == parent ? mPlugins.size() : 0;
}
QVariant PanelPluginsModel::data(const QModelIndex & index, int role/* = Qt::DisplayRole*/) const
{
Q_ASSERT(QModelIndex() == index.parent()
&& 0 == index.column()
&& mPlugins.size() > index.row()
);
pluginslist_t::const_reference plugin = mPlugins[index.row()];
QVariant ret;
switch (role)
{
case Qt::DisplayRole:
if (plugin.second.isNull())
ret = QStringLiteral("<b>Unknown</b> (%1)").arg(plugin.first);
else
ret = QStringLiteral("<b>%1</b> (%2)").arg(plugin.second->name(), plugin.first);
break;
case Qt::DecorationRole:
if (plugin.second.isNull())
ret = XdgIcon::fromTheme(QStringLiteral("preferences-plugin"));
else
ret = plugin.second->desktopFile().icon(XdgIcon::fromTheme(QStringLiteral("preferences-plugin")));
break;
case Qt::UserRole:
ret = QVariant::fromValue(const_cast<Plugin const *>(plugin.second.data()));
break;
}
return ret;
}
Qt::ItemFlags PanelPluginsModel::flags(const QModelIndex & /*index*/) const
{
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
}
QStringList PanelPluginsModel::pluginNames() const
{
QStringList names;
for (auto const & p : mPlugins)
names.append(p.first);
return names;
}
QList<Plugin *> PanelPluginsModel::plugins() const
{
QList<Plugin *> plugins;
for (auto const & p : mPlugins)
if (!p.second.isNull())
plugins.append(p.second.data());
return plugins;
}
Plugin* PanelPluginsModel::pluginByName(QString name) const
{
for (auto const & p : mPlugins)
if (p.first == name)
return p.second.data();
return nullptr;
}
Plugin const * PanelPluginsModel::pluginByID(QString id) const
{
for (auto const & p : mPlugins)
{
Plugin *plugin = p.second.data();
if (plugin && plugin->desktopFile().id() == id)
return plugin;
}
return nullptr;
}
void PanelPluginsModel::addPlugin(const LXQt::PluginInfo &desktopFile)
{
if (dynamic_cast<LXQtPanelApplication const *>(qApp)->isPluginSingletonAndRunning(desktopFile.id()))
return;
QString name = findNewPluginSettingsGroup(desktopFile.id());
QPointer<Plugin> plugin = loadPlugin(desktopFile, name);
if (plugin.isNull())
return;
beginInsertRows(QModelIndex(), mPlugins.size(), mPlugins.size());
mPlugins.append({name, plugin});
endInsertRows();
mPanel->settings()->setValue(mNamesKey, pluginNames());
emit pluginAdded(plugin.data());
}
void PanelPluginsModel::removePlugin(pluginslist_t::iterator plugin)
{
if (mPlugins.end() != plugin)
{
mPanel->settings()->remove(plugin->first);
Plugin * p = plugin->second.data();
const int row = plugin - mPlugins.begin();
beginRemoveRows(QModelIndex(), row, row);
mPlugins.erase(plugin);
endRemoveRows();
emit pluginRemoved(p); // p can be nullptr
mPanel->settings()->setValue(mNamesKey, pluginNames());
if (nullptr != p)
p->deleteLater();
}
}
void PanelPluginsModel::removePlugin()
{
Plugin * p = qobject_cast<Plugin*>(sender());
auto plugin = std::find_if(mPlugins.begin(), mPlugins.end(),
[p] (pluginslist_t::const_reference obj) { return p == obj.second; });
removePlugin(std::move(plugin));
}
void PanelPluginsModel::movePlugin(Plugin * plugin, QString const & nameAfter)
{
//merge list of plugins (try to preserve original position)
//subtract mPlugin.begin() from the found Plugins to get the model index
const int from =
std::find_if(mPlugins.begin(), mPlugins.end(), [plugin] (pluginslist_t::const_reference obj) { return plugin == obj.second.data(); })
- mPlugins.begin();
const int to =
std::find_if(mPlugins.begin(), mPlugins.end(), [nameAfter] (pluginslist_t::const_reference obj) { return nameAfter == obj.first; })
- mPlugins.begin();
/* 'from' is the current position of the Plugin to be moved ("moved Plugin"),
* 'to' is the position of the Plugin behind the one that is being moved
* ("behind Plugin"). There are several cases to distinguish:
* 1. from > to: The moved Plugin had been behind the behind Plugin before
* and is moved to the front of the behind Plugin. The moved Plugin will
* be inserted at position 'to', the behind Plugin and all the following
* Plugins (until the former position of the moved Plugin) will increment
* their indexes.
* 2. from < to: The moved Plugin had already been located before the
* behind Plugin. In this case, the move operation only reorders the
* Plugins before the behind Plugin. All the Plugins between the moved
* Plugin and the behind Plugin will decrement their index. Therefore, the
* movedPlugin will not be at position 'to' but rather on position 'to-1'.
* 3. from == to: This does not make sense, we catch this case to prevent
* errors.
* 4. from == to-1: The moved Plugin has not moved because it had already
* been located in front of the behind Plugin.
*/
const int to_plugins = from < to ? to - 1 : to;
if (from != to && from != to_plugins)
{
/* Although the new position of the moved Plugin will be 'to-1' if
* from < to, we insert 'to' here. This is exactly how it is done
* in the Qt documentation.
*/
beginMoveRows(QModelIndex(), from, from, QModelIndex(), to);
// For the QList::move method, use the right position
mPlugins.move(from, to_plugins);
endMoveRows();
emit pluginMoved(plugin);
mPanel->settings()->setValue(mNamesKey, pluginNames());
}
}
void PanelPluginsModel::loadPlugins(QStringList const & desktopDirs)
{
QStringList plugin_names = mPanel->settings()->value(mNamesKey).toStringList();
#ifdef DEBUG_PLUGIN_LOADTIME
QElapsedTimer timer;
timer.start();
qint64 lastTime = 0;
#endif
for (auto const & name : std::as_const(plugin_names))
{
pluginslist_t::iterator i = mPlugins.insert(mPlugins.end(), {name, nullptr});
QString type = mPanel->settings()->value(name + QStringLiteral("/type")).toString();
if (type.isEmpty())
{
qWarning() << QStringLiteral("Section \"%1\" not found in %2.").arg(name, mPanel->settings()->fileName());
continue;
}
#ifdef WITH_SCREENSAVER_FALLBACK
if (QStringLiteral("screensaver") == type)
{
//plugin-screensaver was dropped
//convert settings to plugin-quicklaunch
const QString & lock_desktop = QStringLiteral(LXQT_LOCK_DESKTOP);
qWarning().noquote() << "Found deprecated plugin of type 'screensaver', migrating to 'quicklaunch' with '" << lock_desktop << '\'';
type = QStringLiteral("quicklaunch");
LXQt::Settings * settings = mPanel->settings();
settings->beginGroup(name);
settings->remove(QString{});//remove all existing keys
settings->setValue(QStringLiteral("type"), type);
settings->beginWriteArray(QStringLiteral("apps"), 1);
settings->setArrayIndex(0);
settings->setValue(QStringLiteral("desktop"), lock_desktop);
settings->endArray();
settings->endGroup();
}
#endif
LXQt::PluginInfoList list = LXQt::PluginInfo::search(desktopDirs, QStringLiteral("LXQtPanel/Plugin"), QStringLiteral("%1.desktop").arg(type));
if( !list.count())
{
qWarning() << QStringLiteral("Plugin \"%1\" not found.").arg(type);
continue;
}
i->second = loadPlugin(list.first(), name);
#ifdef DEBUG_PLUGIN_LOADTIME
qDebug() << "load plugin" << type << "takes" << (timer.elapsed() - lastTime) << "ms";
lastTime = timer.elapsed();
#endif
}
}
QPointer<Plugin> PanelPluginsModel::loadPlugin(LXQt::PluginInfo const & desktopFile, QString const & settingsGroup)
{
std::unique_ptr<Plugin> plugin(new Plugin(desktopFile, mPanel->settings(), settingsGroup, mPanel));
if (plugin->isLoaded())
{
connect(mPanel, &LXQtPanel::realigned, plugin.get(), &Plugin::realign);
connect(plugin.get(), &Plugin::remove,
this, static_cast<void (PanelPluginsModel::*)()>(&PanelPluginsModel::removePlugin));
return plugin.release();
}
return nullptr;
}
QString PanelPluginsModel::findNewPluginSettingsGroup(const QString &pluginType) const
{
QStringList groups = mPanel->settings()->childGroups();
groups.sort();
// Generate new section name
QString pluginName = QStringLiteral("%1").arg(pluginType);
if (!groups.contains(pluginName))
return pluginName;
else
{
for (int i = 2; true; ++i)
{
pluginName = QStringLiteral("%1%2").arg(pluginType).arg(i);
if (!groups.contains(pluginName))
return pluginName;
}
}
}
bool PanelPluginsModel::isIndexValid(QModelIndex const & index) const
{
return index.isValid() && QModelIndex() == index.parent()
&& 0 == index.column() && mPlugins.size() > index.row();
}
void PanelPluginsModel::onMovePluginUp(QModelIndex const & index)
{
if (!isIndexValid(index))
return;
const int row = index.row();
if (0 >= row)
return; //can't move up
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
mPlugins.swapItemsAt(row - 1, row);
endMoveRows();
pluginslist_t::const_reference moved_plugin = mPlugins[row - 1];
pluginslist_t::const_reference prev_plugin = mPlugins[row];
emit pluginMoved(moved_plugin.second.data());
//emit signal for layout only in case both plugins are loaded/displayed
if (!moved_plugin.second.isNull() && !prev_plugin.second.isNull())
emit pluginMovedUp(moved_plugin.second.data());
mPanel->settings()->setValue(mNamesKey, pluginNames());
}
void PanelPluginsModel::onMovePluginDown(QModelIndex const & index)
{
if (!isIndexValid(index))
return;
const int row = index.row();
if (mPlugins.size() <= row + 1)
return; //can't move down
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2);
mPlugins.swapItemsAt(row, row + 1);
endMoveRows();
pluginslist_t::const_reference moved_plugin = mPlugins[row + 1];
pluginslist_t::const_reference next_plugin = mPlugins[row];
emit pluginMoved(moved_plugin.second.data());
//emit signal for layout only in case both plugins are loaded/displayed
if (!moved_plugin.second.isNull() && !next_plugin.second.isNull())
emit pluginMovedUp(next_plugin.second.data());
mPanel->settings()->setValue(mNamesKey, pluginNames());
}
void PanelPluginsModel::onConfigurePlugin(QModelIndex const & index)
{
if (!isIndexValid(index))
return;
Plugin * const plugin = mPlugins[index.row()].second.data();
if (nullptr != plugin && (ILXQtPanelPlugin::HaveConfigDialog & plugin->iPlugin()->flags()))
plugin->showConfigureDialog();
}
void PanelPluginsModel::onRemovePlugin(QModelIndex const & index)
{
if (!isIndexValid(index))
return;
auto plugin = mPlugins.begin() + index.row();
if (plugin->second.isNull())
removePlugin(std::move(plugin));
else
plugin->second->requestRemove();
}
|