-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
102 lines (79 loc) · 2.54 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <QApplication>
#include <QSharedMemory>
#include <QProcess>
#include "mainwindow.h"
#include "../Lib/supportfunctions.h"
#include "../Lib/alertsound.h"
#include <iostream>
#include <QMessageBox>
#ifdef WIN32
#include <windows.h>
#include <winuser.h>
#endif
qint64 masterPID(MainWindow *win, QSharedMemory *sharedMem) ;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Set Application Name
a.setApplicationName("Contact Manager") ;
a.setApplicationVersion(GITHASH) ;
MainWindow w;
int result ;
// Initialise the sound alerts
initSound(NULL) ;
QSharedMemory *sharedMem = new QSharedMemory("ContactManager");
qint64 master = masterPID(&w, sharedMem) ;
if (master>=0) {
// TODO: Signal the PID to tell it to maximise, then exit quietly
// If the signal doesn't get through, give the user a wait a minute message
// Get HWND from PID
// Call SetFocus() for HWND
#ifdef WIN32
WPARAM wp = SC_RESTORE ;
LPARAM lp = 0 ;
if (PostMessage((HWND)master, WM_SYSCOMMAND, wp, lp))
SetForegroundWindow((HWND)master);
else
// Send QMessage
#endif
warningOkDialog(&w, "Contact Manager Already Running",
"Contact Manager is already Running. Press Alt-Tab to find it") ;
result=0 ;
} else {
w.show();
result = a.exec();
}
sharedMem->detach() ;
delete sharedMem ;
return result ;
}
// Get the Process ID of the first app
// If not found, return -1
qint64 masterPID(MainWindow *win, QSharedMemory *sharedMem)
{
Q_UNUSED(win) ;
qint64 PID, MYPID ;
#ifdef WIN32
MYPID = win->winId() ;
#else
MYPID = QCoreApplication::applicationPid() ;
#endif
// Try to create twice.
// This clears stale shared memory if a Unix application has aborted / crashed
if (sharedMem->create(sizeof(qint64)) || sharedMem->create(sizeof(qint64)) ) {
// Created so write PID
void *ptr = sharedMem->data() ;
qint64 *qptr = (qint64 *)ptr ;
*qptr = MYPID ;
PID=-1 ;
} else {
// Already exists, so read PID
sharedMem->attach() ;
void *ptr = sharedMem->data() ;
qint64 *qptr = (qint64*) ptr ;
PID = *qptr ;
// TODO: If the process does not exist, don't detach, write our PID, and return -1
sharedMem->detach() ;
}
return PID ;
}