1 | /* $Id: VBoxHostVersion.cpp 23143 2009-09-18 16:05:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxHostVersion - Checks the host's VirtualBox version and notifies
|
---|
4 | * the user in case of an update.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "VBoxHostVersion.h"
|
---|
24 | #include "VBoxTray.h"
|
---|
25 | #include "helpers.h"
|
---|
26 |
|
---|
27 | #include <VBox/VBoxGuestLib.h>
|
---|
28 |
|
---|
29 | /* Returns 0 if equal, 1 if Ver1 is greater, 2 if Ver2 is greater
|
---|
30 | * Requires strings in form of "majorVer.minorVer.build" */
|
---|
31 | /** @todo should be common code in the guest lib / headers */
|
---|
32 | int VBoxCompareVersion (const char* pszVer1, const char* pszVer2)
|
---|
33 | {
|
---|
34 | int rc = 0;
|
---|
35 | int iVer1Major, iVer1Minor, iVer1Build;
|
---|
36 | sscanf(pszVer1, "%d.%d.%d", &iVer1Major, &iVer1Minor, &iVer1Build);
|
---|
37 | int iVer2Major, iVer2Minor, iVer2Build;
|
---|
38 | sscanf(pszVer2, "%d.%d.%d", &iVer2Major, &iVer2Minor, &iVer2Build);
|
---|
39 |
|
---|
40 | int iVer1Final = (iVer1Major * 10000) + (iVer1Minor * 100) + iVer1Build;
|
---|
41 | int iVer2Final = (iVer2Major * 10000) + (iVer2Minor * 100) + iVer2Build;
|
---|
42 |
|
---|
43 | if (iVer1Final > iVer2Final)
|
---|
44 | rc = 1;
|
---|
45 | else if (iVer2Final > iVer1Final)
|
---|
46 | rc = 2;
|
---|
47 | return rc;
|
---|
48 | }
|
---|
49 |
|
---|
50 | int VBoxCheckHostVersion ()
|
---|
51 | {
|
---|
52 | int rc;
|
---|
53 | uint32_t uGuestPropSvcClientID;
|
---|
54 |
|
---|
55 | rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
|
---|
56 | if (RT_SUCCESS(rc))
|
---|
57 | Log(("VBoxTray: Property Service Client ID: %ld\n", uGuestPropSvcClientID));
|
---|
58 | else
|
---|
59 | Log(("VBoxTray: Failed to connect to the guest property service! Error: %d\n", rc));
|
---|
60 |
|
---|
61 | /* Do we need to do all this stuff? */
|
---|
62 | char *pszCheckHostVersion;
|
---|
63 | rc = VbglR3GuestPropReadValueAlloc(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/CheckHostVersion", &pszCheckHostVersion);
|
---|
64 | if (RT_FAILURE(rc))
|
---|
65 | {
|
---|
66 | if (rc == VERR_NOT_FOUND)
|
---|
67 | rc = VINF_SUCCESS; /* If we don't find the value above we do the check by default */
|
---|
68 | else
|
---|
69 | Log(("VBoxTray: Could not read check host version flag! rc = %d\n", rc));
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | /* Only don't do the check if we have a valid "0" in it */
|
---|
74 | if ( atoi(pszCheckHostVersion) == 0
|
---|
75 | && strlen(pszCheckHostVersion))
|
---|
76 | {
|
---|
77 | rc = VERR_NOT_SUPPORTED;
|
---|
78 | }
|
---|
79 | VbglR3GuestPropReadValueFree(pszCheckHostVersion);
|
---|
80 | }
|
---|
81 | if (rc == VERR_NOT_SUPPORTED)
|
---|
82 | Log(("VBoxTray: No host version check performed."));
|
---|
83 |
|
---|
84 | char szMsg[256] = "\0"; /* Sizes according to MSDN. */
|
---|
85 | char szTitle[64] = "\0";
|
---|
86 |
|
---|
87 | if (RT_SUCCESS(rc))
|
---|
88 | {
|
---|
89 | /* Look up host version (revision) */
|
---|
90 | char *pszVBoxHostVer;
|
---|
91 | rc = VbglR3GuestPropReadValueAlloc(uGuestPropSvcClientID, "/VirtualBox/HostInfo/VBoxVer", &pszVBoxHostVer);
|
---|
92 | if (RT_FAILURE(rc))
|
---|
93 | {
|
---|
94 | Log(("VBoxTray: Could not read VBox host version! rc = %d\n", rc));
|
---|
95 | }
|
---|
96 | else
|
---|
97 | {
|
---|
98 | Log(("VBoxTray: Host version: %s\n", pszVBoxHostVer));
|
---|
99 |
|
---|
100 | /* Look up guest version */
|
---|
101 | char szVBoxGuestVer[32];
|
---|
102 | char szVBoxGuestRev[32];
|
---|
103 |
|
---|
104 | rc = getAdditionsVersion(szVBoxGuestVer, sizeof(szVBoxGuestVer), szVBoxGuestRev, sizeof(szVBoxGuestRev));
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | {
|
---|
107 | Log(("VBoxTray: Additions version: %s\n", szVBoxGuestVer));
|
---|
108 |
|
---|
109 | /* Look up last informed host version */
|
---|
110 | char szVBoxHostVerLastChecked[32];
|
---|
111 | HKEY hKey;
|
---|
112 | LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions\\VBoxTray", 0, KEY_READ, &hKey);
|
---|
113 | if (lRet == ERROR_SUCCESS)
|
---|
114 | {
|
---|
115 | DWORD dwType;
|
---|
116 | DWORD dwSize = sizeof(szVBoxHostVerLastChecked);
|
---|
117 | lRet = RegQueryValueEx(hKey, "HostVerLastChecked", NULL, &dwType, (BYTE*)szVBoxHostVerLastChecked, &dwSize);
|
---|
118 | if (lRet != ERROR_SUCCESS && lRet != ERROR_FILE_NOT_FOUND)
|
---|
119 | Log(("VBoxTray: Could not read HostVerLastChecked! Error = %ld\n", lRet));
|
---|
120 | RegCloseKey(hKey);
|
---|
121 | }
|
---|
122 | else if (lRet != ERROR_FILE_NOT_FOUND)
|
---|
123 | {
|
---|
124 | Log(("VBoxTray: Could not open VBoxTray registry key! Error = %ld\n", lRet));
|
---|
125 | rc = RTErrConvertFromWin32(lRet);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* Compare both versions and prepare message */
|
---|
129 | if ( RT_SUCCESS(rc)
|
---|
130 | && strcmp(pszVBoxHostVer, szVBoxHostVerLastChecked) != 0 /* Make sure we did not process this host version already */
|
---|
131 | && VBoxCompareVersion(pszVBoxHostVer, szVBoxGuestVer) == 1) /* Is host version greater than guest add version? */
|
---|
132 | {
|
---|
133 | /** @todo add some translation macros here */
|
---|
134 | _snprintf(szTitle, sizeof(szTitle), "VirtualBox Guest Additions update available!");
|
---|
135 | _snprintf(szMsg, sizeof(szMsg), "Your guest is currently running the Guest Additions version %s. "
|
---|
136 | "We recommend updating to the latest version (%s) by choosing the "
|
---|
137 | "install option from the Devices menu.", szVBoxGuestVer, pszVBoxHostVer);
|
---|
138 |
|
---|
139 | /* Save the version to just do a balloon once per new version */
|
---|
140 | if (RT_SUCCESS(rc))
|
---|
141 | {
|
---|
142 | lRet = RegCreateKeyEx (HKEY_LOCAL_MACHINE,
|
---|
143 | "SOFTWARE\\Sun\\VirtualBox Guest Additions\\VBoxTray",
|
---|
144 | 0, /* Reserved */
|
---|
145 | NULL, /* lpClass [in, optional] */
|
---|
146 | 0, /* dwOptions [in] */
|
---|
147 | KEY_WRITE,
|
---|
148 | NULL, /* lpSecurityAttributes [in, optional] */
|
---|
149 | &hKey,
|
---|
150 | NULL); /* lpdwDisposition [out, optional] */
|
---|
151 | if (lRet == ERROR_SUCCESS)
|
---|
152 | {
|
---|
153 | lRet = RegSetValueEx(hKey, "HostVerLastChecked", 0, REG_SZ, (BYTE*)pszVBoxHostVer, (DWORD)(strlen(pszVBoxHostVer)*sizeof(char)));
|
---|
154 | if (lRet != ERROR_SUCCESS)
|
---|
155 | Log(("VBoxTray: Could not write HostVerLastChecked! Error = %ld\n", lRet));
|
---|
156 | RegCloseKey(hKey);
|
---|
157 | }
|
---|
158 | else
|
---|
159 | Log(("VBoxTray: Could not open VBoxTray registry key! Error = %ld\n", lRet));
|
---|
160 |
|
---|
161 | if (lRet != ERROR_SUCCESS)
|
---|
162 | rc = RTErrConvertFromWin32(lRet);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | VbglR3GuestPropReadValueFree(pszVBoxHostVer);
|
---|
168 | }
|
---|
169 |
|
---|
170 | if (RT_SUCCESS(rc) && strlen(szMsg))
|
---|
171 | {
|
---|
172 | rc = showBalloonTip(gInstance, gToolWindow, ID_TRAYICON, szMsg, szTitle, 5000, 0);
|
---|
173 | if (RT_FAILURE(rc))
|
---|
174 | Log(("VBoxTray: Could not show version notifier balloon tooltip! rc = %d\n", rc));
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* If we didn't have to check for the host version then this is not an error */
|
---|
178 | if (rc == VERR_NOT_SUPPORTED)
|
---|
179 | rc = VINF_SUCCESS;
|
---|
180 |
|
---|
181 | VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
|
---|
182 | return rc;
|
---|
183 | }
|
---|
184 |
|
---|