Back to index

nordugrid-arc-nox  1.1.0~rc6
paul_gui.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <string>
00006 #define NOGDI
00007 #include <windows.h>
00008 #include <arc/win32.h>
00009 #include <stdio.h>
00010 
00011 #include <arc/ArcConfig.h>
00012 #include "paul_gui.h"
00013 
00014 const char g_szClassName[] = "PaulGUI";
00015 HMENU popup_menu;
00016 bool running = false;
00017 Arc::Config *cfg = NULL;
00018 PROCESS_INFORMATION processinfo;
00019 
00020 static void start_arched(void)
00021 {
00022     if (running) {
00023         return;
00024     }
00025     cfg->parse("paul_gui.xml");
00026     std::string p = (std::string)(*cfg)["ArcPluginPath"];
00027     SetEnvironmentVariable("ARC_PLUGIN_PATH", p.c_str());
00028     std::string path = p + ";%PATH%";
00029     SetEnvironmentVariable("PATH", path.c_str()); 
00030     std::string hed = "\"" + (std::string)(*cfg)["ArcHEDCmd"] + "\"";
00031     std::string cfg_name = "\"" + (std::string)(*cfg)["ArcHEDConfig"] + "\"";
00032     // XXX read ArcHEDConfig and determine status URL
00033     std::string cmd = hed + " -f -c " + cfg_name;
00034     printf ("cmd: %s\n", cmd.c_str());
00035     fflush(stdout);
00036     if (cmd.empty()) {
00037         return;
00038     }
00039     STARTUPINFO startupinfo;
00040     memset(&startupinfo, 0, sizeof(startupinfo));
00041     int result = CreateProcess(NULL, 
00042                              (LPSTR)cmd.c_str(), 
00043                                NULL, 
00044                                NULL, 
00045                                FALSE, 
00046                                CREATE_NEW_PROCESS_GROUP|CREATE_NO_WINDOW,
00047                                NULL, 
00048                                (LPSTR)".",
00049                                &startupinfo, 
00050                                &processinfo);
00051      printf("ret: %d\n", result);
00052      printf("error %s\n", GetOsErrorMessage().c_str());
00053      running = true;
00054      fflush(stdout);
00055 }
00056 
00057 static void stop_arched(void)
00058 {
00059     if (!running) {
00060         return;
00061     }
00062     TerminateProcess(processinfo.hProcess, 256);
00063     running = false;
00064 }
00065 
00066 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
00067 {
00068        switch(msg)
00069        {
00070         case WM_COMMAND:
00071         {   
00072             switch(LOWORD(wParam)) {
00073                 case ID_SETUP: 
00074                 {
00075                     printf("Setup\n");
00076                     fflush(stdout);
00077                     ShellExecute(NULL, "open", "http://localhost:60000/paul", NULL, NULL, SW_SHOWNORMAL); 
00078                     return 0;
00079                 }
00080                 break;
00081                 case ID_CLOSE:
00082                 {
00083                     printf ("close\n");
00084                     fflush(stdout);
00085                     PostMessage(hwnd, WM_CLOSE, 0, 0);
00086                     return 0;
00087                 }
00088                 break;
00089                 case ID_RUN:
00090                 {
00091                     start_arched();
00092                     return 0;
00093                 }
00094                 break;
00095                 case ID_END:
00096                 {
00097                     stop_arched();
00098                     return 0;
00099                 }
00100                 break;
00101             }
00102         }
00103         break;
00104         case WM_TRAYNOTIFY:
00105         {
00106             // catch event on try
00107             UINT uID = (UINT) wParam;
00108             UINT uMouseMsg = (UINT) lParam;
00109             if (uMouseMsg == WM_RBUTTONUP) {
00110                 if (uID == IDI_PAULGUI) {
00111                     HMENU submenu = GetSubMenu(popup_menu, 0);
00112                     if (submenu == NULL) {
00113                         printf("null\n");
00114                     }   
00115                     SetMenuDefaultItem(submenu, 0, TRUE);
00116                     POINT mouse;
00117                     GetCursorPos(&mouse);
00118                     TrackPopupMenu(submenu, 0, mouse.x, mouse.y, 0, hwnd, NULL);                    fflush(stdout);
00119                     return 0;
00120                 }
00121             }
00122         }
00123         break;
00124               case WM_CREATE:
00125               {
00126             // hide the created window
00127             ShowWindow(hwnd, SW_HIDE);
00128               }
00129               break;
00130               break;
00131               case WM_CLOSE:
00132         {
00133             stop_arched();
00134                      DestroyWindow(hwnd);
00135         }
00136               break;
00137               case WM_DESTROY:
00138         {
00139             // Unregister Tray icon
00140             NOTIFYICONDATA tnd;
00141             tnd.cbSize = sizeof(NOTIFYICONDATA);
00142             tnd.hWnd = hwnd;
00143             tnd.uID = IDI_PAULGUI;
00144             Shell_NotifyIcon(NIM_DELETE, &tnd);
00145                      PostQuitMessage(0);
00146         }
00147               break;
00148               default:
00149                      return DefWindowProc(hwnd, msg, wParam, lParam);
00150        }
00151        return 0;
00152 }
00153 
00154 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
00155        LPSTR lpCmdLine, int nCmdShow)
00156 {
00157     cfg = new Arc::Config();
00158        
00159     WNDCLASSEX wc;
00160        HWND hwnd;
00161        MSG Msg;
00162 
00163        wc.cbSize             = sizeof(WNDCLASSEX);
00164        wc.style              = 0;
00165        wc.lpfnWndProc        = WndProc;
00166        wc.cbClsExtra  = 0;
00167        wc.cbWndExtra  = 0;
00168        wc.hInstance   = hInstance;
00169        wc.hIcon              = LoadIcon(NULL, IDI_APPLICATION);
00170        wc.hCursor            = LoadCursor(NULL, IDC_ARROW);
00171        wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
00172        wc.lpszMenuName  = NULL;
00173        wc.lpszClassName = g_szClassName;
00174        wc.hIconSm            = LoadIcon(NULL, IDI_APPLICATION);
00175 
00176        if(!RegisterClassEx(&wc))
00177        {
00178               MessageBox(NULL, "Window Registration Failed!", "Error!",
00179                      MB_ICONEXCLAMATION | MB_OK);
00180               return 0;
00181        }
00182 
00183        hwnd = CreateWindowEx(
00184               0,
00185               g_szClassName,
00186               "Arc - PAUL Service GUI",
00187               WS_OVERLAPPEDWINDOW,
00188               CW_USEDEFAULT, CW_USEDEFAULT, 480, 320,
00189               NULL, NULL, hInstance, NULL);
00190 
00191        if(hwnd == NULL)
00192        {
00193               MessageBox(NULL, "Window Creation Failed!", "Error!",
00194                      MB_ICONEXCLAMATION | MB_OK);
00195               return 0;
00196        }
00197 
00198        ShowWindow(hwnd, SW_HIDE);
00199        UpdateWindow(hwnd);
00200         
00201     // Load menu
00202     popup_menu = LoadMenu(hInstance, MAKEINTRESOURCE(IDR_TRAYMENU));
00203     if (popup_menu == NULL) {
00204         printf("Error load menu!\n");
00205         fflush(stdout);
00206     }
00207     // System tray
00208     NOTIFYICONDATA tnd;
00209     tnd.cbSize = sizeof(NOTIFYICONDATA);
00210     tnd.hWnd = hwnd;
00211     tnd.uID = IDI_PAULGUI;
00212     tnd.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
00213     tnd.uCallbackMessage = WM_TRAYNOTIFY;
00214     tnd.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_PAULGUI)); 
00215     strncpy(tnd.szTip, "ARC - Paul Configurator", sizeof(tnd.szTip));
00216     tnd.szTip[sizeof(tnd.szTip)-1]=0;
00217     Shell_NotifyIcon(NIM_ADD, &tnd);
00218     EnableMenuItem(popup_menu, ID_CLOSE, MF_ENABLED);
00219     
00220     // start arched
00221     start_arched();
00222        while(GetMessage(&Msg, NULL, 0, 0) > 0)
00223        {
00224               TranslateMessage(&Msg);
00225               DispatchMessage(&Msg);
00226        }
00227        return Msg.wParam;
00228 }