TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musictimerautomodule.cpp
Go to the documentation of this file.
1 #include "musictimerautomodule.h"
2 #include "musicsettingmanager.h"
3 #include "musicapplication.h"
4 #include "musiccoreutils.h"
5 
6 #include <QTime>
7 #ifdef Q_OS_WIN
8 # define WIN32_LEAN_AND_MEAN
9 # include <qt_windows.h>
10 #else
11 # include <signal.h>
12 # include <unistd.h>
13 # include <sys/reboot.h>
14 #endif
15 
17  : QObject(parent)
18 {
19  MusicTimeData play, stop, shutdown;
20  play.m_index = 0;
21  stop.m_index = 1;
22  shutdown.m_index = 2;
23  m_timeDatas << play << stop << shutdown;
24 
25  connect(&m_timer, SIGNAL(timeout()), SLOT(timeout()));
26 }
27 
29 {
30  m_timer.stop();
31 }
32 
34 {
35  m_timer.start(TTK_DN_S2MS);
36 
38  {
39  m_timeDatas[0].m_state = true;
42  }
43 
45  {
46  m_timeDatas[1].m_state = true;
49  }
50 
52  {
53  m_timeDatas[2].m_state = true;
56  }
57 }
58 
60 {
61  int hour = 0, minute = 0;
62  const QStringList &l = QTime::currentTime().toString(Qt::ISODate).split(":");
63  if(l.count() != 3)
64  {
65  return;
66  }
67 
68  hour = l[0].toInt();
69  minute = l[1].toInt();
70 
71  for(int i = 0; i < m_timeDatas.count(); ++i)
72  {
73  MusicTimeData *pair = &m_timeDatas[i];
74  if(pair->m_state && hour == pair->m_hour && minute == pair->m_minute)
75  {
76  switch(pair->m_index)
77  {
78  case 0:
79  {
81  {
82  pair->m_state = false;
84  }
86  break;
87  }
88  case 1:
89  {
91  {
92  pair->m_state = false;
94  }
96  break;
97  }
98  case 2:
99  {
101  {
102  pair->m_state = false;
104  }
105  setShutdown();
106  break;
107  }
108  default: break;
109  }
110  }
111  }
112 }
113 
115 {
116 #ifdef Q_OS_WIN
117  HANDLE hToken;
118  TOKEN_PRIVILEGES tkp;
119  if(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
120  {
121  LookupPrivilegeValue(nullptr, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
122  tkp.PrivilegeCount = 1;
123  tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
124  AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, TTKStaticCast(PTOKEN_PRIVILEGES, nullptr), nullptr);
125  ExitWindowsEx(EWX_SHUTDOWN | EWX_POWEROFF, 0);
126  }
127 #elif defined Q_OS_UNIX
128  /* first disable all our signals */
129  sigset_t set;
130  sigfillset(&set);
131  sigprocmask(SIG_BLOCK, &set, nullptr);
132  /* send signals to all processes _except_ pid 1 */
133  kill(-1, SIGTERM);
134  sync();
136 
137  kill(-1, SIGKILL);
138  sync();
140  /* shutdown */
141  reboot(RB_POWER_OFF);
142 #endif
143  TTK_INFO_STREAM("Shutdown now");
144 }
#define TTKStaticCast(x, y)
Definition: ttkglobal.h:159
static MusicApplication * instance()
void setPlaySongChanged(int index)
MusicTimeDataList m_timeDatas
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:67
TTK_MODULE_EXPORT void sleep(int ms)
#define TTK_DN_S2MS
Definition: ttkglobal.h:276
MusicTimerAutoModule(QObject *parent=nullptr)
#define G_SETTING_PTR