Windows: conditionally set tray icon state
[openafs.git] / src / WINNT / client_creds / shortcut.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afsconfig.h>
15 #include <afs/param.h>
16 #include <roken.h>
17 #include <afs/stds.h>
18 }
19
20 #include <objbase.h>
21 #include <initguid.h>
22 #include <windows.h>
23 #include <windowsx.h>
24 #undef INITGUID
25 #include <shlobj.h>
26 #include <shellapi.h>
27 #include <shobjidl.h>
28 #include <shlguid.h>
29 #include "afscreds.h"
30 #include "shortcut.h"
31
32
33 /*
34  * ROUTINES ___________________________________________________________________
35  *
36  */
37
38 void Shortcut_Init (void)
39 {
40    CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
41 }
42
43
44 void Shortcut_Exit (void)
45 {
46    CoUninitialize();
47 }
48
49 BOOL Shortcut_Create (LPTSTR pszTarget, LPCTSTR pszSource, LPTSTR pszDesc, LPTSTR pszArgs)
50 {
51    IShellLink *psl;
52    HRESULT rc = CoCreateInstance (CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **)&psl);
53    if (SUCCEEDED (rc))
54       {
55       IPersistFile *ppf;
56       rc = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
57       if (SUCCEEDED (rc))
58          { 
59          rc = psl->SetPath (pszSource);
60          if (SUCCEEDED (rc))
61             {
62             rc = psl->SetDescription (pszDesc ? pszDesc : pszSource);
63             if (SUCCEEDED (rc))
64                {
65                if ( pszArgs )
66                    rc = psl->SetArguments (pszArgs);
67                    if (SUCCEEDED (rc))
68                    {
69 #ifdef UNICODE
70                    rc = ppf->Save (pszTarget, TRUE);
71 #else
72                    WORD wsz[ MAX_PATH ];
73                    MultiByteToWideChar (CP_ACP, 0, pszTarget, -1, (LPWSTR)wsz, MAX_PATH);
74                    rc = ppf->Save ((LPCOLESTR)wsz, TRUE);
75 #endif
76                    }
77                }
78             }
79          ppf->Release ();
80          }
81       psl->Release ();
82       }
83    return SUCCEEDED(rc) ? TRUE : FALSE;
84
85
86
87 BOOL Shortcut_FixStartup (LPCTSTR pszLinkName, BOOL fAutoStart)
88 {
89    TCHAR szShortcut[ MAX_PATH + 10 ] = TEXT("");
90    BOOL bSuccess;
91
92    HKEY hk;
93    if (RegOpenKey (HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"), &hk) == 0)
94       {
95       DWORD dwSize = sizeof(szShortcut);
96       DWORD dwType = REG_SZ;
97       RegQueryValueEx (hk, TEXT("Common Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
98       if (szShortcut[0] == TEXT('\0'))
99          {
100          dwSize = sizeof(szShortcut);
101          dwType = REG_SZ;
102          RegQueryValueEx (hk, TEXT("Startup"), NULL, &dwType, (LPBYTE)szShortcut, &dwSize);
103          }
104       RegCloseKey (hk);
105       }
106    if (szShortcut[0] == TEXT('\0'))
107       {
108       GetWindowsDirectory (szShortcut, MAX_PATH);
109       lstrcat (szShortcut, TEXT("\\Start Menu\\Programs\\Startup"));
110       }
111    lstrcat (szShortcut, TEXT("\\"));
112    lstrcat (szShortcut, pszLinkName);
113
114    TCHAR szSource[ MAX_PATH ];
115    GetModuleFileName (GetModuleHandle(NULL), szSource, MAX_PATH);
116
117    if (fAutoStart)
118    {
119        DWORD code, len, type; 
120        TCHAR szParams[ 64 ] = TEXT(AFSCREDS_SHORTCUT_OPTIONS);
121
122        code = RegOpenKeyEx(HKEY_CURRENT_USER, AFSREG_USER_OPENAFS_SUBKEY,
123                             0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk);
124        if (code == ERROR_SUCCESS) {
125            len = sizeof(szParams);
126            type = REG_SZ;
127            code = RegQueryValueEx(hk, "AfscredsShortcutParams", NULL, &type,
128                                    (BYTE *) &szParams, &len);
129            RegCloseKey (hk);
130        }
131        if (code != ERROR_SUCCESS) {
132            code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
133                                 0, (IsWow64()?KEY_WOW64_64KEY:0)|KEY_QUERY_VALUE, &hk);
134            if (code == ERROR_SUCCESS) {
135                len = sizeof(szParams);
136                type = REG_SZ;
137                code = RegQueryValueEx(hk, "AfscredsShortcutParams", NULL, &type,
138                                        (BYTE *) &szParams, &len);
139                RegCloseKey (hk);
140            }
141        }
142        bSuccess = Shortcut_Create (szShortcut, szSource, "Autostart Authentication Agent", szParams);
143    }
144    else // (!g.fAutoStart)
145    {
146       bSuccess = DeleteFile (szShortcut);
147    }
148
149    return bSuccess;
150 }
151