1 | /* $Id: VBoxHostVersion.cpp 23115 2009-09-18 08:27:42Z 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 = VERR_NOT_SUPPORTED; /* If we don't find the value above this is not critical */
|
---|
68 | else
|
---|
69 | Log(("VBoxTray: Could not read check host version flag! rc = %d\n", rc));
|
---|
70 | }
|
---|
71 | else
|
---|
72 | {
|
---|
73 | if (pszCheckHostVersion && atoi(pszCheckHostVersion) <= 0)
|
---|
74 | rc = VERR_NOT_SUPPORTED;
|
---|
75 | VbglR3GuestPropReadValueFree(pszCheckHostVersion);
|
---|
76 | }
|
---|
77 | if (rc == VERR_NOT_SUPPORTED)
|
---|
78 | Log(("VBoxTray: No host version check performed."));
|
---|
79 |
|
---|
80 | char szMsg[256] = "\0"; /* Sizes according to MSDN. */
|
---|
81 | char szTitle[64] = "\0";
|
---|
82 |
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | {
|
---|
85 | /* Look up host version (revision) */
|
---|
86 | char *pszVBoxHostVer;
|
---|
87 | rc = VbglR3GuestPropReadValueAlloc(uGuestPropSvcClientID, "/VirtualBox/HostInfo/VBoxVer", &pszVBoxHostVer);
|
---|
88 | if (RT_FAILURE(rc))
|
---|
89 | {
|
---|
90 | Log(("VBoxTray: Could not read VBox host version! rc = %d\n", rc));
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | Log(("VBoxTray: Host version: %s\n", pszVBoxHostVer));
|
---|
95 |
|
---|
96 | /* Look up guest version */
|
---|
97 | char szVBoxGuestVer[32];
|
---|
98 | char szVBoxGuestRev[32];
|
---|
99 |
|
---|
100 | rc = getAdditionsVersion(szVBoxGuestVer, sizeof(szVBoxGuestVer), szVBoxGuestRev, sizeof(szVBoxGuestRev));
|
---|
101 | if (RT_SUCCESS(rc))
|
---|
102 | {
|
---|
103 | Log(("VBoxTray: Additions version: %s\n", szVBoxGuestVer));
|
---|
104 |
|
---|
105 | /* Look up last informed host version */
|
---|
106 | char szVBoxHostVerLastChecked[32];
|
---|
107 | HKEY hKey;
|
---|
108 | LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Sun\\VirtualBox Guest Additions\\VBoxTray", 0, KEY_READ, &hKey);
|
---|
109 | if (lRet == ERROR_SUCCESS)
|
---|
110 | {
|
---|
111 | DWORD dwType;
|
---|
112 | DWORD dwSize = sizeof(szVBoxHostVerLastChecked);
|
---|
113 | lRet = RegQueryValueEx(hKey, "HostVerLastChecked", NULL, &dwType, (BYTE*)szVBoxHostVerLastChecked, &dwSize);
|
---|
114 | if (lRet != ERROR_SUCCESS && lRet != ERROR_FILE_NOT_FOUND)
|
---|
115 | Log(("VBoxTray: Could not read HostVerLastChecked! Error = %ld\n", lRet));
|
---|
116 | RegCloseKey(hKey);
|
---|
117 | }
|
---|
118 | else if (lRet != ERROR_FILE_NOT_FOUND)
|
---|
119 | {
|
---|
120 | Log(("VBoxTray: Could not open VBoxTray registry key! Error = %ld\n", lRet));
|
---|
121 | rc = RTErrConvertFromWin32(lRet);
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Compare both versions and prepare message */
|
---|
125 | if ( RT_SUCCESS(rc)
|
---|
126 | && strcmp(pszVBoxHostVer, szVBoxHostVerLastChecked) != 0 /* Make sure we did not process this host version already */
|
---|
127 | && VBoxCompareVersion(pszVBoxHostVer, szVBoxGuestVer) == 1) /* Is host version greater than guest add version? */
|
---|
128 | {
|
---|
129 | /** @todo add some translation macros here */
|
---|
130 | _snprintf(szTitle, sizeof(szTitle), "VirtualBox Guest Additions update available!");
|
---|
131 | _snprintf(szMsg, sizeof(szMsg), "Your guest is currently running the Guest Additions version %s. "
|
---|
132 | "We recommend updating to the latest version (%s) by choosing the "
|
---|
133 | "install option from the Devices menu.", szVBoxGuestVer, pszVBoxHostVer);
|
---|
134 |
|
---|
135 | /* Save the version to just do a balloon once per new version */
|
---|
136 | if (RT_SUCCESS(rc))
|
---|
137 | {
|
---|
138 | lRet = RegCreateKeyEx (HKEY_LOCAL_MACHINE,
|
---|
139 | "SOFTWARE\\Sun\\VirtualBox Guest Additions\\VBoxTray",
|
---|
140 | 0, /* Reserved */
|
---|
141 | NULL, /* lpClass [in, optional] */
|
---|
142 | 0, /* dwOptions [in] */
|
---|
143 | KEY_WRITE,
|
---|
144 | NULL, /* lpSecurityAttributes [in, optional] */
|
---|
145 | &hKey,
|
---|
146 | NULL); /* lpdwDisposition [out, optional] */
|
---|
147 | if (lRet == ERROR_SUCCESS)
|
---|
148 | {
|
---|
149 | lRet = RegSetValueEx(hKey, "HostVerLastChecked", 0, REG_SZ, (BYTE*)pszVBoxHostVer, (DWORD)(strlen(pszVBoxHostVer)*sizeof(char)));
|
---|
150 | if (lRet != ERROR_SUCCESS)
|
---|
151 | Log(("VBoxTray: Could not write HostVerLastChecked! Error = %ld\n", lRet));
|
---|
152 | RegCloseKey(hKey);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | Log(("VBoxTray: Could not open VBoxTray registry key! Error = %ld\n", lRet));
|
---|
156 |
|
---|
157 | if (lRet != ERROR_SUCCESS)
|
---|
158 | rc = RTErrConvertFromWin32(lRet);
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | VbglR3GuestPropReadValueFree(pszVBoxHostVer);
|
---|
164 | }
|
---|
165 |
|
---|
166 | if (RT_SUCCESS(rc) && strlen(szMsg))
|
---|
167 | {
|
---|
168 | rc = showBalloonTip(gInstance, gToolWindow, ID_TRAYICON, szMsg, szTitle, 5000, 0);
|
---|
169 | if (RT_FAILURE(rc))
|
---|
170 | Log(("VBoxTray: Could not show version notifier balloon tooltip! rc = %d\n", rc));
|
---|
171 | }
|
---|
172 |
|
---|
173 | /* If we didn't have to check for the host version then this is not an error */
|
---|
174 | if (rc == VERR_NOT_SUPPORTED)
|
---|
175 | rc = VINF_SUCCESS;
|
---|
176 |
|
---|
177 | VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|