Removed some legacy stuff.
[nssm.git] / gui.cpp
1 #include "nssm.h"\r
2 \r
3 int nssm_gui(int resource, char *name) {\r
4   char blurb[256];\r
5 \r
6   /* Create window */\r
7   HWND dlg = CreateDialog(0, MAKEINTRESOURCE(resource), 0, install_dlg);\r
8   if (! dlg) {\r
9     _snprintf(blurb, sizeof(blurb), "CreateDialog() failed with error code %d", GetLastError());\r
10     MessageBox(0, blurb, NSSM, MB_OK);\r
11     return 1;\r
12   }\r
13 \r
14   /* Display the window */\r
15   centre_window(dlg);\r
16   ShowWindow(dlg, SW_SHOW);\r
17 \r
18   /* Set service name if given */\r
19   if (name) {\r
20     SetDlgItemText(dlg, IDC_NAME, name);\r
21     /* No point making user click remove if the name is already entered */\r
22     if (resource == IDD_REMOVE) {\r
23       HWND button = GetDlgItem(dlg, IDC_REMOVE);\r
24       if (button) {\r
25         SendMessage(button, WM_LBUTTONDOWN, 0, 0);\r
26         SendMessage(button, WM_LBUTTONUP, 0, 0);\r
27       }\r
28     }\r
29   }\r
30 \r
31   /* Go! */\r
32   MSG message;\r
33   while (GetMessage(&message, 0, 0, 0)) {\r
34     TranslateMessage(&message);\r
35     DispatchMessage(&message);\r
36   }\r
37 \r
38   return (int) message.wParam;\r
39 }\r
40 \r
41 void centre_window(HWND window) {\r
42   HWND desktop;\r
43   RECT size, desktop_size;\r
44   unsigned long x, y;\r
45 \r
46   if (! window) return;\r
47 \r
48   /* Find window size */\r
49   if (! GetWindowRect(window, &size)) return;\r
50   \r
51   /* Find desktop window */\r
52   desktop = GetDesktopWindow();\r
53   if (! desktop) return;\r
54 \r
55   /* Find desktop window size */\r
56   if (! GetWindowRect(desktop, &desktop_size)) return;\r
57 \r
58   /* Centre window */\r
59   x = (desktop_size.right - size.right) / 2;\r
60   y = (desktop_size.bottom - size.bottom) / 2;\r
61   MoveWindow(window, x, y, size.right - size.left, size.bottom - size.top, 0);\r
62 }\r
63 \r
64 /* Install the service */\r
65 int install(HWND window) {\r
66   if (! window) return 1;\r
67 \r
68   /* Check parameters in the window */\r
69   char name[STRING_SIZE];\r
70   char exe[EXE_LENGTH];\r
71   char flags[STRING_SIZE];\r
72 \r
73   /* Get service name */\r
74   if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {\r
75     MessageBox(0, "No valid service name was specified!", NSSM, MB_OK);\r
76     return 2;\r
77   }\r
78 \r
79   /* Get executable name */\r
80   if (! GetDlgItemText(window, IDC_PATH, exe, sizeof(exe))) {\r
81     MessageBox(0, "No valid executable path was specified!", NSSM, MB_OK);\r
82     return 3;\r
83   }\r
84 \r
85   /* Get flags */\r
86   if (SendMessage(GetDlgItem(window, IDC_FLAGS), WM_GETTEXTLENGTH, 0, 0)) {\r
87     if (! GetDlgItemText(window, IDC_FLAGS, flags, sizeof(flags))) {\r
88       MessageBox(0, "No valid options were specified!", NSSM, MB_OK);\r
89       return 4;\r
90     }\r
91   }\r
92   else ZeroMemory(&flags, sizeof(flags));\r
93 \r
94   /* See if it works */\r
95   switch (install_service(name, exe, flags)) {\r
96     case 2:\r
97       MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\r
98       return 2;\r
99 \r
100     case 3:\r
101       MessageBox(0, "Path too long!\nThe full path to " NSSM " is too long.\nPlease install " NSSM " somewhere else...\n", NSSM, MB_OK);\r
102       return 3;\r
103 \r
104     case 4:\r
105       MessageBox(0, "Error constructing ImagePath!\nThis really shouldn't happen.  You could be out of memory\nor the world may be about to end or something equally bad.", NSSM, MB_OK);\r
106       return 4;\r
107 \r
108     case 5:\r
109       MessageBox(0, "Couldn't create service!\nPerhaps it is already installed...", NSSM, MB_OK);\r
110       return 5;\r
111 \r
112     case 6:\r
113       MessageBox(0, "Couldn't set startup parameters for the service!\nDeleting the service...", NSSM, MB_OK);\r
114       return 6;\r
115   }\r
116 \r
117   MessageBox(0, "Service successfully installed!", NSSM, MB_OK);\r
118   return 0;\r
119 }\r
120 \r
121 /* Remove the service */\r
122 int remove(HWND window) {\r
123   if (! window) return 1;\r
124 \r
125   /* Check parameters in the window */\r
126   char name[STRING_SIZE];\r
127 \r
128   /* Get service name */\r
129   if (! GetDlgItemText(window, IDC_NAME, name, sizeof(name))) {\r
130     MessageBox(0, "No valid service name was specified!", NSSM, MB_OK);\r
131     return 2;\r
132   }\r
133 \r
134   /* Confirm */\r
135   char blurb[MAX_PATH];\r
136   if (_snprintf(blurb, sizeof(blurb), "Remove the \"%s\" service?", name) < 0) {\r
137     if (MessageBox(0, "Remove the service?", NSSM, MB_YESNO) != IDYES) return 0;\r
138   }\r
139   else if (MessageBox(0, blurb, NSSM, MB_YESNO) != IDYES) return 0;\r
140 \r
141   /* See if it works */\r
142   switch (remove_service(name)) {\r
143     case 2:\r
144       MessageBox(0, "Can't open service manager!\nPerhaps you need to be an administrator...", NSSM, MB_OK);\r
145       return 2;\r
146 \r
147     case 3:\r
148       MessageBox(0, "Can't open service!\nPerhaps it isn't installed...", NSSM, MB_OK);\r
149       return 3;\r
150 \r
151     case 4:\r
152       MessageBox(0, "Can't delete service!  Make sure the service is stopped and try again.\nIf this error persists, you may need to set the service NOT to start\nautomatically, reboot your computer and try removing it again.", NSSM, MB_OK);\r
153       return 4;\r
154   }\r
155 \r
156   MessageBox(0, "Service successfully removed!", NSSM, MB_OK);\r
157   return 0;\r
158 }\r
159 \r
160 /* Browse for game */\r
161 void browse(HWND window) {\r
162   if (! window) return;\r
163 \r
164   OPENFILENAME ofn;\r
165   ZeroMemory(&ofn, sizeof(ofn));\r
166   ofn.lStructSize = sizeof(ofn);\r
167   ofn.lpstrFilter = "Applications\0*.exe\0All files\0*.*\0\0";\r
168   ofn.lpstrFile = new char[MAX_PATH];\r
169   ofn.lpstrFile[0] = '\0';\r
170   ofn.lpstrTitle = "Locate application file";\r
171   ofn.nMaxFile = MAX_PATH;\r
172   ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;\r
173 \r
174   if (GetOpenFileName(&ofn)) {\r
175     SendMessage(window, WM_SETTEXT, 0, (LPARAM) ofn.lpstrFile);\r
176   }\r
177 \r
178   delete[] ofn.lpstrFile;\r
179 }\r
180 \r
181 /* Install/remove dialogue callback */\r
182 INT_PTR CALLBACK install_dlg(HWND window, UINT message, WPARAM w, LPARAM l) {\r
183   switch (message) {\r
184     /* Creating the dialogue */\r
185     case WM_INITDIALOG:\r
186       return 1;\r
187 \r
188     /* Button was pressed or control was controlled */\r
189     case WM_COMMAND:\r
190       switch (LOWORD(w)) {\r
191         /* OK button */\r
192         case IDC_OK:\r
193           PostQuitMessage(install(window));\r
194           break;\r
195 \r
196         /* Cancel button */\r
197         case IDC_CANCEL:\r
198           DestroyWindow(window);\r
199           break;\r
200 \r
201         /* Browse button */\r
202         case IDC_BROWSE:\r
203           browse(GetDlgItem(window, IDC_PATH));\r
204           break;\r
205 \r
206         /* Remove button */\r
207         case IDC_REMOVE:\r
208           PostQuitMessage(remove(window));\r
209           break;\r
210       }\r
211       return 1;\r
212 \r
213     /* Window closing */\r
214     case WM_CLOSE:\r
215       DestroyWindow(window);\r
216       return 0;\r
217     case WM_DESTROY:\r
218       PostQuitMessage(0);\r
219   }\r
220   return 0;\r
221 }\r