Skip to content
Browse files

Add autostart entry API into the library

  • Loading branch information...
1 parent 5477099 commit cc1f6b93c06bd9b18fcf15e2aa39e7f078aa118e @amoskvin amoskvin committed Dec 25, 2012
View
3 libraries/razorqt/CMakeLists.txt
@@ -27,6 +27,7 @@ set(razorqt_PUB_HDRS
razorconfigdialog.h
razorpower/razorpower.h
razornotification.h
+ razorautostartentry.h
translatorsinfo/translatorsinfo.h
)
@@ -55,8 +56,8 @@ set(razorqt_SRCS
razorpower/razorpower.cpp
razorpower/razorpowerproviders.cpp
razornotification.cpp
+ razorautostartentry.cpp
translatorsinfo/translatorsinfo.cpp
-
)
set(razorqt_MOCS
View
126 libraries/razorqt/razorautostartentry.cpp
@@ -0,0 +1,126 @@
+/* BEGIN_COMMON_COPYRIGHT_HEADER
+ * (c)LGPL2+
+ *
+ * Razor - a lightweight, Qt based, desktop toolset
+ * http://razor-qt.org
+ *
+ * Copyright (C) 2012 Alec Moskvin <alecm@gmx.com>
+ *
+ * This 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 "razorautostartentry.h"
+#include <qtxdg/xdgautostart.h>
+#include <qtxdg/xdgdirs.h>
+#include <QtCore/QFileInfo>
+
+RazorAutostartEntry::RazorAutostartEntry() :
+ mLocalState(StateNone), mSystem(false)
+{
+}
+
+RazorAutostartEntry::RazorAutostartEntry(const QString& name):
+ mLocalState(StateNone), mSystem(false)
+{
+ foreach (const QString& dir, XdgDirs::autostartDirs())
+ {
+ QString path = QString("%1/%2").arg(dir, name);
+ if (QFile(path).exists())
+ {
+ mSystemFile.load(path);
+ mSystem = true;
+ break;
+ }
+ }
+
+ QString path = QString("%1/%2").arg(XdgDirs::autostartHome(), name);
+ if (QFile(path).exists())
+ {
+ mLocalFile.load(path);
+ mLocalState = StateExists;
+ }
+}
+
+void RazorAutostartEntry::setFile(const XdgDesktopFile& file)
+{
+ bool local = isLocal();
+ if (mSystem && local && file == mSystemFile)
+ {
+ removeLocal();
+ }
+ else
+ {
+ if (local)
+ mLocalState = StateModified;
+ else
+ mLocalState = StateTransient;
+ mLocalFile = file;
+ }
+}
+
+bool RazorAutostartEntry::removeLocal()
+{
+ if (!isLocal())
+ return false;
+
+ if (mLocalState == StateTransient)
+ mLocalState = StateNone;
+ else
+ mLocalState = StateDeleted;
+
+ return !mSystem;
+}
+
+const XdgDesktopFile& RazorAutostartEntry::file() const
+{
+ return isLocal() ? mLocalFile : mSystemFile;
+}
+
+QString RazorAutostartEntry::name() const
+{
+ return QFileInfo(file().fileName()).fileName();
+}
+
+void RazorAutostartEntry::setEnabled(bool enable)
+{
+ XdgDesktopFile f = file();
+ if (enable)
+ f.removeEntry("Hidden");
+ else
+ f.setValue("Hidden", true);
+
+ setFile(f);
+}
+
+bool RazorAutostartEntry::isEnabled() const
+{
+ return !file().value("Hidden", false).toBool();
+}
+
+bool RazorAutostartEntry::commit()
+{
+ if (mLocalState == StateDeleted)
+ {
+ mLocalState = StateNone;
+ return QFile::remove(mLocalFile.fileName());
+ }
+ else if (mLocalState == StateModified || mLocalState == StateTransient)
+ {
+ mLocalState = StateExists;
+ return mLocalFile.save(XdgAutoStart::localPath(mLocalFile));
+ }
+ return true;
+}
View
109 libraries/razorqt/razorautostartentry.h
@@ -0,0 +1,109 @@
+/* BEGIN_COMMON_COPYRIGHT_HEADER
+ * (c)LGPL2+
+ *
+ * Razor - a lightweight, Qt based, desktop toolset
+ * http://razor-qt.org
+ *
+ * Copyright (C) 2012 Alec Moskvin <alecm@gmx.com>
+ *
+ * This 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 */
+
+#ifndef RAZORAUTOSTARTENTRY_H
+#define RAZORAUTOSTARTENTRY_H
+
+#include <qtxdg/xdgdesktopfile.h>
+
+/*! \brief The AutostartEntry class provides an interface for staging configuration of individual
+autostart items. All changes are made in memory until commit() is called.
+
+ - "system" file refers to a read-only file in /etc/xdg/autostart (or a directory in $XDG_CONFIG_DIRS)
+ - "local" file refers to the file in user's ~/.config/autostart (or in $XDG_CONFIG_HOME/autostart)
+
+When a "local" file has the same name as the "system" file, the local one overrides it. This class
+tries to ensure that the "local" file is deleted if it's identical to the "system" file.
+*/
+class RazorAutostartEntry
+{
+public:
+ /*! Constructs an AutostartEntry object for a specific entry.
+ * \param name The name of the autostart desktop file (e.g. "razor-panel.desktop")
+ */
+ RazorAutostartEntry(const QString& name);
+
+ //! \brief Default constructor
+ RazorAutostartEntry();
+
+ //! Destructor
+ virtual ~RazorAutostartEntry() { }
+
+ //! Returns the "active" desktop file
+ const XdgDesktopFile& file() const;
+
+ //! Returns the name of the autostart entry (e.g. "razor-panel.desktop")
+ QString name() const;
+
+ /*! Sets to the specified desktop file. Use this to make modifications.
+ * \param file The desktop file
+ */
+ void setFile(const XdgDesktopFile& file);
+
+ /*! Sets whether the item auto-starts
+ * \param enable When false, sets the "Hidden" key which will prevent the item from starting
+ */
+ void setEnabled(bool enable);
+
+ //! Returns true if the item will auto-start
+ bool isEnabled() const;
+
+ /*! Returns true if the entry does not exist, and the object carries no useful information
+ * and can be ignored/deleted.
+ */
+ bool isEmpty() const { return !mSystem && mLocalState == StateNone; }
+
+ /*! Write any changes to disk
+ * \return true on success
+ */
+ bool commit();
+
+protected:
+ //! Returns true if the user's "local" version exists
+ bool isLocal() const { return mLocalState != StateNone && mLocalState != StateDeleted; }
+
+ /*! Removes the user's "local" version of the file, reverting to system defaults
+ * \return true if there is no "system" version left (i.e. the entry was entirely deleted)
+ */
+ bool removeLocal();
+
+ //! \brief a read-only file in /etc/xdg/autostart (or a directory in $XDG_CONFIG_DIRS)
+ XdgDesktopFile mSystemFile;
+
+ //! \brief the file in user's ~/.config/autostart (or in $XDG_CONFIG_HOME/autostart)
+ XdgDesktopFile mLocalFile;
+
+ //! State of the "local" file
+ enum ItemState
+ {
+ StateNone, //! does not exist at all
+ StateDeleted, //! needs to be deleted from disk
+ StateTransient, //! does not yet exist on disk
+ StateModified, //! exists on disk and is modified
+ StateExists //! exists on disk and unmodified
+ } mLocalState;
+ bool mSystem; //! true if the "system" file exists
+};
+
+#endif // RAZORAUTOSTARTENTRY_H
View
76 razorqt-session/razor-config-session/autostartitem.cpp
@@ -28,34 +28,15 @@
#include <QtCore/QFileInfo>
AutostartItem::AutostartItem() :
- mLocalState(StateNone), mSystem(false)
+ RazorAutostartEntry()
{
}
AutostartItem::AutostartItem(const XdgDesktopFile& systemFile) :
- mSystemFile(systemFile), mLocalState(StateNone), mSystem(true)
+ RazorAutostartEntry()
{
-}
-
-AutostartItem::~AutostartItem()
-{
-}
-
-void AutostartItem::setLocal(const XdgDesktopFile& file)
-{
- bool local = isLocal();
- if (mSystem && local && file == mSystemFile)
- {
- removeLocal();
- }
- else
- {
- if (local)
- mLocalState = StateModified;
- else
- mLocalState = StateTransient;
- mLocalFile = file;
- }
+ mSystemFile = systemFile;
+ mSystem = true;
}
void AutostartItem::setLocalFromFile(const XdgDesktopFile& file)
@@ -64,55 +45,6 @@ void AutostartItem::setLocalFromFile(const XdgDesktopFile& file)
mLocalState = StateExists;
}
-bool AutostartItem::removeLocal()
-{
- if (!isLocal())
- return false;
-
- if (mLocalState == StateTransient)
- mLocalState = StateNone;
- else
- mLocalState = StateDeleted;
-
- return !mSystem;
-}
-
-const XdgDesktopFile& AutostartItem::file() const
-{
- return isLocal() ? mLocalFile : mSystemFile;
-}
-
-void AutostartItem::setEnabled(bool enable)
-{
- XdgDesktopFile f = file();
- if (enable)
- f.removeEntry("Hidden");
- else
- f.setValue("Hidden", true);
-
- setLocal(f);
-}
-
-bool AutostartItem::isEnabled() const
-{
- return !file().value("Hidden", false).toBool();
-}
-
-bool AutostartItem::commit()
-{
- if (mLocalState == StateDeleted)
- {
- mLocalState = StateNone;
- return QFile::remove(mLocalFile.fileName());
- }
- else if (mLocalState == StateModified || mLocalState == StateTransient)
- {
- mLocalState = StateExists;
- return mLocalFile.save(XdgAutoStart::localPath(mLocalFile));
- }
- return true;
-}
-
QMap<QString,AutostartItem> AutostartItem::createItemMap()
{
QMap<QString,AutostartItem> items;
View
48 razorqt-session/razor-config-session/autostartitem.h
@@ -25,7 +25,7 @@
#ifndef AUTOSTARTITEM_H
#define AUTOSTARTITEM_H
-#include <qtxdg/xdgdesktopfile.h>
+#include <razorqt/razorautostartentry.h>
/*! \brief The AutostartItem class provides an interface for staging configuration of individual
autostart items. All changes are made in memory until commit() is called.
@@ -37,7 +37,7 @@ When a "local" file has the same name as the "system" file, the local one overri
tries to ensure that the "local" file is deleted if it's the same as the "system" file.
*/
-class AutostartItem
+class AutostartItem : public RazorAutostartEntry
{
public:
//! \brief Default constructor
@@ -48,70 +48,30 @@ class AutostartItem
*/
AutostartItem(const XdgDesktopFile& systemFile);
- //! Destructor
- ~AutostartItem();
-
- //! Returns the "active" desktop file
- const XdgDesktopFile& file() const;
-
//! Returns the "system" file
const XdgDesktopFile& systemfile() const { return mSystemFile; }
- /*! Sets the local version to the specified desktop file. Use this to make modifications.
- * \param file the desktop file
- */
- void setLocal(const XdgDesktopFile& file);
-
/*! Sets the local version that exists on disk
* \param file the desktop file, must already exist in user's autostart directory
*/
void setLocalFromFile(const XdgDesktopFile &file);
- /*! Sets whether the item auto-starts
- * \param enable When false, sets the "Hidden" key which will prevent the item from starting
- */
- void setEnabled(bool enable);
-
/*! Removes the "local" version of the file
* \return true if there is no "system" version left (i.e. the item was entirely deleted)
*/
- bool removeLocal();
-
- //! Returns true if the item will auto-start
- bool isEnabled() const;
+ bool removeLocal() { return RazorAutostartEntry::removeLocal(); }
//! Returns true if both the "local" and "system" versions exist
bool overrides() const { return mSystem && isLocal(); }
//! Returns true if the local version exists
- bool isLocal() const { return mLocalState != StateNone && mLocalState != StateDeleted; }
+ bool isLocal() const { return RazorAutostartEntry::isLocal(); }
//! Returns true if the local file does not exist on disk
bool isTransient() const { return mLocalState == StateTransient; }
- //! Returns true if this object carries no useful information and can be ignored/deleted
- bool isEmpty() const { return !mSystem && mLocalState == StateNone; }
-
- /*! Write any changes to disk
- * \return true on success
- */
- bool commit();
-
//! Creates a mapping of file name (a unique ID) to AutostartItem for the running system
static QMap<QString, AutostartItem> createItemMap();
-
-private:
- XdgDesktopFile mSystemFile;
- XdgDesktopFile mLocalFile;
- enum ItemState // State of the "local" file
- {
- StateNone, // does not exist at all
- StateDeleted, // needs to be deleted from disk
- StateTransient, // does not yet exist on disk
- StateModified, // exists on disk and is modified
- StateExists // exists on disk and unmodified
- } mLocalState;
- bool mSystem; // whether the "system" file exists
};
#endif // AUTOSTARTITEM_H
View
2 razorqt-session/razor-config-session/autostartmodel.cpp
@@ -82,7 +82,7 @@ bool AutoStartItemModel::setEntry(const QModelIndex& index, XdgDesktopFile entry
if (parent == mRazorIndex)
entry.setValue("OnlyShowIn", "Razor;");
- mItemMap[fileName].setLocal(entry);
+ mItemMap[fileName].setFile(entry);
if (replacing)
{

0 comments on commit cc1f6b9

Please sign in to comment.
Something went wrong with that request. Please try again.