Commit b5110073 authored by Raphael Rosch's avatar Raphael Rosch Committed by Stefano Crocco
Browse files

Improve custom sidebar button functionality

This fixes a couple issues, adds session persistence,
 creates a context menu option to restore deleted default buttons,
 and adds the ability to store the last used button to the OpenViews
 so it opens that automatically the next time the session is opened
 (but really, this needs to be moved to the session file).
parent b9ea60ce
......@@ -112,6 +112,20 @@ QString ModuleManager::moduleFullPath(const QString &fileName) const
return QStandardPaths::locate(QStandardPaths::GenericDataLocation, moduleDataPath(fileName));
}
void ModuleManager::saveOpenViews(const QStringList &fileName)
{
// TODO: this would be best stored per-window, in the session file
m_config->writeEntry("OpenViews", fileName);
m_config->sync();
}
void ModuleManager::restoreDeletedButtons()
{
m_config->writeEntry("DeletedModules", QStringList());
m_config->sync();
}
void ModuleManager::rollbackToDefault()
{
const QString loc = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/konqsidebartng/";
......@@ -126,6 +140,7 @@ void ModuleManager::rollbackToDefault()
}
m_config->writeEntry("DeletedModules", QStringList());
m_config->writeEntry("AddedModules", QStringList());
m_config->sync();
}
void ModuleManager::setModuleName(const QString &fileName, const QString &moduleName)
......@@ -162,6 +177,30 @@ void ModuleManager::setShowHiddenFolders(const QString &fileName, const bool &ne
ksc.sync();
}
int ModuleManager::getMaxKDEWeight()
{
int curMax = 1; // 0 is reserved for the treeModule
for (const QString &fileName : modules()) {
const QString path = moduleDataPath(fileName);
if (! QStandardPaths::locate(QStandardPaths::GenericDataLocation, path).isEmpty()) {
KSharedConfig::Ptr config = KSharedConfig::openConfig(path,
KConfig::NoGlobals,
QStandardPaths::GenericDataLocation);
KConfigGroup configGroup(config, "Desktop Entry");
const int weight = configGroup.readEntry("X-KDE-Weight", 0);
if (curMax < weight) {
curMax = weight;
}
}
}
return curMax;
}
int ModuleManager::getNextAvailableKDEWeight()
{
return getMaxKDEWeight() + 1;
}
void ModuleManager::removeModule(const QString &fileName)
{
// Remove the local file (if it exists)
......@@ -171,12 +210,14 @@ void ModuleManager::removeModule(const QString &fileName)
// Mark module as deleted (so that we skip global file, if there's one)
QStringList deletedModules = m_config->readEntry("DeletedModules", QStringList());
QStringList addedModules = m_config->readEntry("AddedModules", QStringList());
if (!deletedModules.contains(fileName)) {
if ( !addedModules.contains(fileName) && !deletedModules.contains(fileName)) { // only add it to the "deletedModules" list if it is a global module
deletedModules.append(fileName);
}
addedModules.removeAll(fileName);
m_config->writeEntry("DeletedModules", deletedModules);
m_config->writeEntry("AddedModules", addedModules);
m_config->sync();
}
void ModuleManager::moduleAdded(const QString &fileName)
......@@ -190,6 +231,7 @@ void ModuleManager::moduleAdded(const QString &fileName)
deletedModules.removeAll(fileName);
m_config->writeEntry("DeletedModules", deletedModules);
m_config->writeEntry("AddedModules", addedModules);
m_config->sync();
}
QString ModuleManager::addModuleFromTemplate(QString &templ)
......
......@@ -56,12 +56,16 @@ public:
/// Returns the full path for a given module. TEMP HACK, TO BE REMOVED
QString moduleFullPath(const QString &fileName) const;
void saveOpenViews(const QStringList &fileName);
void restoreDeletedButtons();
void rollbackToDefault();
void setModuleName(const QString &fileName, const QString &moduleName);
void setModuleUrl(const QString &fileName, const QUrl &url);
void setModuleIcon(const QString &fileName, const QString &icon);
void setShowHiddenFolders(const QString &fileName, const bool &newState);
int getMaxKDEWeight();
int getNextAvailableKDEWeight();
/// Find a unique filename for a new module, based on a template name
/// like "dirtree%1.desktop".
......
......@@ -94,6 +94,7 @@ void Sidebar_Widget::aboutToShowAddMenu()
}
}
m_addMenu->addSeparator();
m_addMenu->addAction(KStandardGuiItem::defaults().icon(), i18n("Restore All Removed Default Buttons"), this, &Sidebar_Widget::slotRestoreDeletedButtons);
m_addMenu->addAction(KStandardGuiItem::defaults().icon(), i18n("Rollback to System Default"), this, &Sidebar_Widget::slotRollback);
}
......@@ -115,6 +116,7 @@ void Sidebar_Widget::triggeredAddMenu(QAction *action)
qCDebug(SIDEBAR_LOG) << myFile << "filename=" << templ;
KDesktopFile df(myFile);
KConfigGroup configGroup = df.desktopGroup();
configGroup.writeEntry("X-KDE-Weight", m_moduleManager.getNextAvailableKDEWeight());
const bool ok = plugin->createNewModule(action->data(), configGroup, this, QVariant());
df.sync();
if (ok) {
......@@ -199,6 +201,8 @@ bool Sidebar_Widget::createDirectModule(const QString &templ,
if (!treeModule.isEmpty()) {
scf.writeEntry("X-KDE-TreeModule", treeModule);
}
int maxKDEWeight = m_moduleManager.getNextAvailableKDEWeight();
scf.writeEntry("X-KDE-Weight", maxKDEWeight); // because modules with same weight as an already displayed module are inaccessible.
scf.sync();
m_moduleManager.moduleAdded(filename);
QTimer::singleShot(0, this, &Sidebar_Widget::updateButtons);
......@@ -225,6 +229,12 @@ void Sidebar_Widget::addWebSideBar(const QUrl &url, const QString &name)
createDirectModule("websidebarplugin%1.desktop", name, url, "internet-web-browser", "konqsidebar_web");
}
void Sidebar_Widget::slotRestoreDeletedButtons()
{
m_moduleManager.restoreDeletedButtons();
QTimer::singleShot(0, this, &Sidebar_Widget::updateButtons);
}
void Sidebar_Widget::slotRollback()
{
if (KMessageBox::warningContinueCancel(this, i18n("<qt>This removes all your entries from the sidebar and adds the system default ones.<br /><b>This procedure is irreversible.</b><br />Do you want to proceed?</qt>")) == KMessageBox::Continue) {
......@@ -648,6 +658,7 @@ void Sidebar_Widget::showHidePage(int page)
openUrl(m_storedCurViewUrl); // also runs the buttonInfo.module->openUrl()
m_visibleViews << buttonInfo.file;
m_latestViewed = page;
m_moduleManager.saveOpenViews(m_visibleViews); // TODO: this would be best stored per-window, in the session file
};
if (!buttonInfo.dock) {
......
......@@ -103,6 +103,7 @@ protected Q_SLOTS:
void showHidePage(int value);
void createButtons();
void updateButtons();
void slotRestoreDeletedButtons();
void slotRollback();
void aboutToShowConfigMenu();
void saveConfig();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment