lxqtscreensaver: Makes it a opaque pointer class
authorLuís Pereira <luis.artur.pereira@gmail.com>
Thu, 30 Jun 2016 19:01:04 +0000 (20:01 +0100)
committerLuís Pereira <luis.artur.pereira@gmail.com>
Fri, 1 Jul 2016 11:44:42 +0000 (12:44 +0100)
It helps with where the implementation details of a library may be hidden
from its users and changes to the implementation can be made to a library
without breaking binary compatibility.

lxqtscreensaver.cpp
lxqtscreensaver.h

index 02e88c4..17e7192 100644 (file)
 #include <XdgIcon>
 #include <QMessageBox>
 #include <QAction>
+#include <QPointer>
+#include <QProcess>
+#include <QCoreApplication> // for Q_DECLARE_TR_FUNCTIONS
 
-using namespace LXQt;
+namespace LXQt {
 
-ScreenSaver::ScreenSaver(QObject * parent)
-    : QObject(parent)
+class ScreenSaverPrivate
 {
-    m_xdgProcess = new QProcess(this);
-    connect(m_xdgProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
-            this, SLOT(xdgProcess_finished(int,QProcess::ExitStatus)));
-}
+    Q_DECLARE_TR_FUNCTIONS(LXQt::ScreenSaver);
+    Q_DECLARE_PUBLIC(ScreenSaver)
+    ScreenSaver* const q_ptr;
 
-QList<QAction*> ScreenSaver::availableActions()
-{
-    QList<QAction*> ret;
-    QAction * act;
+public:
+    ScreenSaverPrivate(ScreenSaver *q) : q_ptr(q) {};
 
-    act = new QAction(XdgIcon::fromTheme("system-lock-screen", "lock"), tr("Lock Screen"), this);
-    connect(act, SIGNAL(triggered()), this, SLOT(lockScreen()));
-    ret.append(act);
+    void _l_xdgProcess_finished(int, QProcess::ExitStatus);
+    QPointer<QProcess> m_xdgProcess;
+};
 
-    return ret;
-}
-
-void ScreenSaver::lockScreen()
-{
-    m_xdgProcess->start("xdg-screensaver", QStringList() << "lock");
-}
-
-void ScreenSaver::xdgProcess_finished(int err, QProcess::ExitStatus status)
+void ScreenSaverPrivate::_l_xdgProcess_finished(int err, QProcess::ExitStatus status)
 {
     // http://portland.freedesktop.org/xdg-utils-1.1.0-rc1/scripts/xdg-screensaver
 
+    Q_Q(ScreenSaver);
     if (err == 0)
-        emit activated();
+        emit q->activated();
     else
     {
         QMessageBox *box = new QMessageBox;
@@ -102,6 +94,43 @@ void ScreenSaver::xdgProcess_finished(int err, QProcess::ExitStatus status)
         box->exec();
     }
 
-    emit done();
+    emit q->done();
+}
+
+
+ScreenSaver::ScreenSaver(QObject * parent)
+    : QObject(parent),
+      d_ptr(new ScreenSaverPrivate(this))
+{
+    Q_D(ScreenSaver);
+    d->m_xdgProcess = new QProcess(this);
+    connect(d->m_xdgProcess, SIGNAL(finished(int,QProcess::ExitStatus)),
+            this, SLOT(_l_xdgProcess_finished(int,QProcess::ExitStatus)));
+}
+
+ScreenSaver::~ScreenSaver()
+{
+    delete d_ptr;
 }
 
+QList<QAction*> ScreenSaver::availableActions()
+{
+    QList<QAction*> ret;
+    QAction * act;
+
+    act = new QAction(XdgIcon::fromTheme("system-lock-screen", "lock"), tr("Lock Screen"), this);
+    connect(act, SIGNAL(triggered()), this, SLOT(lockScreen()));
+    ret.append(act);
+
+    return ret;
+}
+
+void ScreenSaver::lockScreen()
+{
+    Q_D(ScreenSaver);
+    d->m_xdgProcess->start("xdg-screensaver", QStringList() << "lock");
+}
+
+} // namespace LXQt
+
+#include "moc_lxqtscreensaver.cpp"
index d2f5ca3..7292092 100644 (file)
 
 #include "lxqtglobals.h"
 #include <QObject>
-#include <QProcess>
 #include <QAction>
 
 namespace LXQt
 {
 
+class ScreenSaverPrivate;
+
 class LXQT_API ScreenSaver : public QObject
 {
     Q_OBJECT
+    Q_DECLARE_PRIVATE(ScreenSaver)
+    Q_DISABLE_COPY(ScreenSaver)
 
 public:
     ScreenSaver(QObject * parent=0);
+    ~ScreenSaver();
 
     QList<QAction*> availableActions();
 
@@ -52,11 +56,9 @@ public slots:
     void lockScreen();
 
 private:
-    QProcess * m_xdgProcess;
-
-private slots:
-    void xdgProcess_finished(int err, QProcess::ExitStatus status);
+    ScreenSaverPrivate* const d_ptr;
 
+    Q_PRIVATE_SLOT(d_func(), void _l_xdgProcess_finished(int, QProcess::ExitStatus))
 };
 
 } // namespace LXQt