source: src-qt4/libpcbsd/utils/pcbsd-sysFlags.h @ cab198c

releng/10.0.2releng/10.0.3
Last change on this file since cab198c was cab198c, checked in by Ken Moore <ken@…>, 6 months ago

Add a new file/classes to libpcbsd - utils.
1) SystemFlags? : public enumeration of different flag types, and a static function to set a particular system flag (SystemFlags::setFlat(SYSFLAG, QString contents) )
2) SystemFlagWatcher? : Class that can be used in Qt utilites for alerting when a new system flag is available (as well as a public function to look for new flags within the last X minutes - for when a utility just started up)

Still needs testing in an actual utility.
One known restriction: setFlag() needs to be run by a root-permissions utility after computer startup before non-root utilities can successfully set flags. This is because the /tmp/pcbsdflags dir needs to be initially created first with 666 permissions.

  • Property mode set to 100644
File size: 1.7 KB
Line 
1#ifndef _PCBSD_SYSTEM_FLAGS_H
2#define _PCBSD_SYSTEM_FLAGS_H
3
4#include <QObject>
5#include <QFileSystemWatcher>
6#include <QString>
7#include <QDateTime>
8#include <QFileInfo>
9#include <QFile>
10#include <QDir>
11#include <QTimer>
12
13#include "pcbsd-utils.h"
14
15// DEFINE THE FLAG FILES
16#define FLAGDIR QString("/tmp/pcbsdflags")
17#define NETWORKRESTARTED QString("network-restarted")
18#define PKGUPDATEAVAILABLE QString("pkg-update-available")
19#define SYSUPDATEAVAILABLE QString("sys-update-available")
20#define PBIUPDATEAVAILABLE QString("pbi-update-available")
21
22class SystemFlags{
23public:
24        enum SYSFLAG{ NetRestart, PkgUpdate, SysUpdate, PbiUpdate};
25       
26        static void setFlag( SYSFLAG flag, QString contents = ""){
27          if(!QFile::exists(FLAGDIR)){
28            pcbsd::Utils::runShellCommand("mkdir -p -m 666 "+FLAGDIR);
29            if( !QFile::exists(FLAGDIR) ){ return; }
30          }
31          QString cmd;
32          if(contents.isEmpty()){ cmd = "touch %1"; }
33          else{ cmd = "echo '%2' > %1"; cmd = cmd.arg("%1", contents); }
34          //Now get the flag type
35          cmd = cmd.arg(FLAGDIR+"/");
36          switch(flag){
37            case NetRestart:
38                cmd = cmd.arg(NETWORKRESTARTED);
39                break;
40            case PkgUpdate:
41                cmd = cmd.arg(PKGUPDATEAVAILABLE);
42                break;
43            case SysUpdate:
44                cmd = cmd.arg(SYSUPDATEAVAILABLE);
45                break;
46            case PbiUpdate:
47                cmd = cmd.arg(PBIUPDATEAVAILABLE);
48                break;
49            default:
50                return;
51          }
52          pcbsd::Utils::runShellCommand(cmd);
53        }
54
55};
56
57class SystemFlagWatcher : public QObject{
58        Q_OBJECT
59public:
60        SystemFlagWatcher();
61        ~SystemFlagWatcher();
62
63        void checkForRecent(int minutes);
64
65private:
66        QDateTime CDT;  //current date time
67        QFileSystemWatcher *watcher;
68
69private slots:
70        void watchFlagDir();
71        void watcherNotification();
72
73signals:
74        void FlagChanged(SystemFlags::SYSFLAG, QString);
75};
76
77#endif
Note: See TracBrowser for help on using the repository browser.