| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 1998 AbiSource, Inc. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License |
| 6 |
* as published by the Free Software Foundation; either version 2 |
| 7 |
* of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
* 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <windows.h> |
| 21 |
#include <commctrl.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "ut_types.h" |
| 27 |
#include "ut_assert.h" |
| 28 |
#include "ut_debugmsg.h" |
| 29 |
#include "ut_string.h" |
| 30 |
#include "ut_go_file.h" |
| 31 |
#include "ut_Win32LocaleString.h" |
| 32 |
#include "xap_Frame.h" |
| 33 |
#include "xap_Win32App.h" |
| 34 |
#include "xap_Win32Slurp.h" |
| 35 |
#include "xap_Prefs.h" |
| 36 |
|
| 37 |
////////////////////////////////////////////////////////////////// |
| 38 |
// "Slurp" (aka "Leech") refers to the ability of an application |
| 39 |
// to receive DDE messages to open a document. For example, the |
| 40 |
// user can double-click on a foo.abw document in Windows Explorer |
| 41 |
// and (if we're properly registered with the system) it will hand |
| 42 |
// the document to us (either to an existing process or launch an |
| 43 |
// instance and then hand it to us). |
| 44 |
// |
| 45 |
// The less-than-polite name for this file reflects that amount of |
| 46 |
// pain endured in writing the code -- and the amount of time |
| 47 |
// required to find any documentation on the subject (search MSDN |
| 48 |
// for: "SAMPLE: How to Use File Associations", Article ID Q1222787, |
| 49 |
// August 5, 1996. |
| 50 |
// |
| 51 |
// It appears that the above cited document is not quite correct |
| 52 |
// (or rather is not aging well)... The examples show setting |
| 53 |
// on "HKEY_CLASSES_ROOT\<foo>" to "File_assoc", but this string |
| 54 |
// is the value used by WindowsExplorer and the FileOpen dialog |
| 55 |
// to describe the document type. |
| 56 |
////////////////////////////////////////////////////////////////// |
| 57 |
|
| 58 |
#define MY_DDE_TOPICNAME L"System" |
| 59 |
|
| 60 |
// DDE does not provide us with an instance handle in the callback |
| 61 |
// to get back to our class member data. Therefore (and since we |
| 62 |
// are only creating one of these classes), we make a static reference |
| 63 |
// to the only instance we create. |
| 64 |
|
| 65 |
static XAP_Win32Slurp * s_Slurp = NULL; |
| 66 |
|
| 67 |
////////////////////////////////////////////////////////////////// |
| 68 |
|
| 69 |
XAP_Win32Slurp::XAP_Win32Slurp(XAP_Win32App * pApp) |
| 70 |
{ |
| 71 |
UT_ASSERT(!s_Slurp); |
| 72 |
|
| 73 |
m_pApp = pApp; |
| 74 |
m_bInitialized = false; |
| 75 |
s_Slurp = this; |
| 76 |
m_hszServerName = NULL; |
| 77 |
m_hszTopic = NULL; |
| 78 |
} |
| 79 |
|
| 80 |
XAP_Win32Slurp::~XAP_Win32Slurp(void) |
| 81 |
{ |
| 82 |
UT_ASSERT_HARMLESS(s_Slurp); |
| 83 |
s_Slurp = NULL; |
| 84 |
} |
| 85 |
|
| 86 |
static HDDEDATA CALLBACK s_DdeServerCallback(UINT uType, UINT uFmt, HCONV hConv, |
| 87 |
HSZ hsz1, HSZ hsz2, HDDEDATA hData, |
| 88 |
DWORD dwData1, DWORD dwData2) |
| 89 |
{ |
| 90 |
return s_Slurp->doCallback(uType,uFmt,hConv,hsz1,hsz2,hData,dwData1,dwData2); |
| 91 |
} |
| 92 |
|
| 93 |
HDDEDATA CALLBACK XAP_Win32Slurp::doCallback(UINT uType, UINT /*uFmt*/, HCONV /*hConv*/, |
| 94 |
HSZ hsz1, HSZ hsz2, HDDEDATA hData, |
| 95 |
DWORD /*dwData1*/, DWORD /*dwData2*/) |
| 96 |
{ |
| 97 |
#ifdef DEBUG |
| 98 |
UINT xtypf = (uType & 0x000f); |
| 99 |
UINT xtyp = (uType & XTYP_MASK); |
| 100 |
UINT xclass = (uType & XCLASS_MASK); |
| 101 |
|
| 102 |
UT_DEBUGMSG(("DDEML callback received [0x%x 0x%x 0x%x]\n",xclass,xtyp,xtypf)); |
| 103 |
#endif |
| 104 |
|
| 105 |
switch (uType) |
| 106 |
{ |
| 107 |
case XTYP_DISCONNECT: |
| 108 |
return 0; |
| 109 |
|
| 110 |
case XTYP_CONNECT: |
| 111 |
{ |
| 112 |
// grant connection request iff it matches my [ServerName,Topic] |
| 113 |
bool bAccept = ( (hsz2==m_hszServerName) && (hsz1==m_hszTopic) ); |
| 114 |
UT_DEBUGMSG(("DDEML connect [accepted %d]\n",bAccept)); |
| 115 |
return (HDDEDATA)bAccept; |
| 116 |
} |
| 117 |
|
| 118 |
case XTYP_EXECUTE: |
| 119 |
processCommand(hData); |
| 120 |
return (HDDEDATA)DDE_FACK; // we handled the command |
| 121 |
|
| 122 |
case XTYP_ERROR: |
| 123 |
UT_DEBUGMSG(("DDEML error in callback\n")); |
| 124 |
return 0; |
| 125 |
|
| 126 |
default: |
| 127 |
return 0; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
bool XAP_Win32Slurp::connectSlurper(void) |
| 132 |
{ |
| 133 |
if (m_bInitialized) // only do this once !! |
| 134 |
return false; |
| 135 |
|
| 136 |
// Register us with the DDE Management Library (DDEML). |
| 137 |
|
| 138 |
m_idDdeServerInst = 0; |
| 139 |
UINT nDdeRtnStatus = DdeInitializeW(&m_idDdeServerInst, |
| 140 |
(PFNCALLBACK)&s_DdeServerCallback, |
| 141 |
( APPCMD_FILTERINITS |
| 142 |
| CBF_SKIP_CONNECT_CONFIRMS |
| 143 |
| CBF_FAIL_SELFCONNECTIONS |
| 144 |
| CBF_FAIL_POKES), |
| 145 |
0); |
| 146 |
if (nDdeRtnStatus != DMLERR_NO_ERROR) |
| 147 |
{ |
| 148 |
UT_DEBUGMSG(("Unable to initialize DDEML [%d]\n",nDdeRtnStatus)); |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
// create two registered strings. |
| 153 |
// the "ServerName" must match the value in the registry under the key: |
| 154 |
// HKEY_CLASSES_ROOT\<xxx>\shell\open\ddeexec\application |
| 155 |
// |
| 156 |
// the "TopicName" must match the value in the key: |
| 157 |
// HKEY_CLASSES_ROOT\<xxx>\shell\open\ddeexec\topic |
| 158 |
|
| 159 |
m_hszServerName = DdeCreateStringHandleW(m_idDdeServerInst, |
| 160 |
L"Abiword"/*const_cast<char *>(m_pApp->getApplicationName())*/, |
| 161 |
CP_WINUNICODE); |
| 162 |
m_hszTopic = DdeCreateStringHandleW(m_idDdeServerInst, MY_DDE_TOPICNAME, CP_WINUNICODE); |
| 163 |
|
| 164 |
// register the server Name |
| 165 |
HDDEDATA bRegistered = DdeNameService(m_idDdeServerInst, m_hszServerName, NULL, DNS_REGISTER); |
| 166 |
if (!bRegistered) |
| 167 |
{ |
| 168 |
UT_DEBUGMSG(("Unable to register NameService with DDEML.\n")); |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
m_bInitialized = true; |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
bool XAP_Win32Slurp::disconnectSlurper(void) |
| 177 |
{ |
| 178 |
if (m_hszServerName) |
| 179 |
DdeFreeStringHandle(m_idDdeServerInst,m_hszServerName); |
| 180 |
if (m_hszTopic) |
| 181 |
DdeFreeStringHandle(m_idDdeServerInst,m_hszTopic); |
| 182 |
|
| 183 |
DdeUninitialize(m_idDdeServerInst); |
| 184 |
m_idDdeServerInst = 0; |
| 185 |
|
| 186 |
m_bInitialized = false; |
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
void XAP_Win32Slurp::processCommand(HDDEDATA hData) |
| 191 |
{ |
| 192 |
DWORD bufSize = DdeGetData(hData,NULL,0,0); |
| 193 |
|
| 194 |
char * pBuf = (char *)UT_calloc(sizeof(char),bufSize+100); |
| 195 |
if (!pBuf) |
| 196 |
{ |
| 197 |
UT_DEBUGMSG(("No memory to allocate DDE buffer [size %d]\n",bufSize)); |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
DdeGetData(hData,(LPBYTE)pBuf,bufSize+99,0); |
| 202 |
UT_Win32LocaleString wstr; |
| 203 |
UT_UTF8String astr; |
| 204 |
wstr.fromLocale((LPCWSTR)pBuf); |
| 205 |
astr=wstr.utf8_str(); |
| 206 |
UT_DEBUGMSG(("DDEML received command '%s'\n",astr.utf8_str())); |
| 207 |
|
| 208 |
// we expect something of the form: |
| 209 |
// [Open("<pathname>")] |
| 210 |
// if anything more complicated is needed, it may be a |
| 211 |
// good idea to use a regex library |
| 212 |
// TODO failures just goto Finished. Some error reporting |
| 213 |
// TODO would be nice |
| 214 |
|
| 215 |
// pointer to work through the incoming string |
| 216 |
const char * next = astr.utf8_str(); |
| 217 |
|
| 218 |
// pointer used to copy into command and pathname |
| 219 |
char * dest = 0; |
| 220 |
|
| 221 |
// chomp the [ |
| 222 |
if ( *next++ != '[' ) goto Finished; |
| 223 |
|
| 224 |
// find the next sequence of non ( characters |
| 225 |
// this will be the dde command |
| 226 |
char command[1024]; |
| 227 |
dest = command; |
| 228 |
for ( ; *next != '('; ++next ) |
| 229 |
{ |
| 230 |
*dest++ = *next; |
| 231 |
} |
| 232 |
*dest = 0; |
| 233 |
|
| 234 |
// chomp the ( and the " |
| 235 |
if ( *next++ != '(' ) goto Finished; |
| 236 |
if ( *next++ != '"' ) goto Finished; |
| 237 |
// go until the next " to get the parameter |
| 238 |
// " are not allowed in filenames, so we should be safe here |
| 239 |
char pathname[4096]; |
| 240 |
dest = pathname; |
| 241 |
for ( ; *next != '"'; ++next ) |
| 242 |
{ |
| 243 |
*dest++ = *next; |
| 244 |
} |
| 245 |
*dest = 0; |
| 246 |
|
| 247 |
// chomp the ", ), and ] |
| 248 |
if ( *next++ != '"' ) goto Finished; |
| 249 |
if ( *next++ != ')' ) goto Finished; |
| 250 |
if ( *next++ != ']' ) goto Finished; |
| 251 |
|
| 252 |
// now do something useful with the command and its parameter |
| 253 |
if (g_ascii_strcasecmp(command,"open") == 0) |
| 254 |
{ |
| 255 |
if (!*pathname) |
| 256 |
{ |
| 257 |
UT_DEBUGMSG(("No pathname given in DDE Open command.\n")); |
| 258 |
goto Finished; |
| 259 |
} |
| 260 |
|
| 261 |
// ask the application to load this document into a window.... |
| 262 |
|
| 263 |
// let's create uri for comparison with filenames from command line |
| 264 |
// TODO: That method does not always work. Some proper method should |
| 265 |
// be designed. |
| 266 |
char *uri = UT_go_filename_to_uri(pathname); |
| 267 |
XAP_Win32App *p_app = (XAP_Win32App *)XAP_App::getApp(); |
| 268 |
UT_sint32 ndx = p_app->findFrame(uri); |
| 269 |
UT_Error error; |
| 270 |
if ((ndx < 0) || p_app->getFrame(ndx)->isDirty()) { |
| 271 |
error = p_app->fileOpen(p_app->getLastFocussedFrame(), uri); |
| 272 |
} |
| 273 |
|
| 274 |
if(error != UT_OK) |
| 275 |
{ |
| 276 |
UT_DEBUGMSG(("Could not load document given in DDE Open command [%s].\n",uri)); |
| 277 |
} |
| 278 |
|
| 279 |
FREEP(uri); |
| 280 |
|
| 281 |
goto Finished; |
| 282 |
} |
| 283 |
|
| 284 |
Finished: |
| 285 |
FREEP(pBuf); |
| 286 |
DdeFreeDataHandle(hData); |
| 287 |
} |
| 288 |
|
| 289 |
/////////////////////////////////////////////////////////////////// |
| 290 |
// Stuff to deal with loading the registry with our file |
| 291 |
// associations and DDE commands. |
| 292 |
/////////////////////////////////////////////////////////////////// |
| 293 |
|
| 294 |
typedef enum { X_Error, X_CreatedKey, X_ExistingKey } XX_Status; |
| 295 |
|
| 296 |
static XX_Status _fetchKey(HKEY k1, const char * szSubKey, HKEY * pkNew) |
| 297 |
{ |
| 298 |
DWORD dwDisposition; |
| 299 |
LONG eResult = RegCreateKeyEx(k1, szSubKey, 0, NULL, |
| 300 |
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, |
| 301 |
NULL, |
| 302 |
pkNew, &dwDisposition); |
| 303 |
if (eResult != ERROR_SUCCESS) |
| 304 |
{ |
| 305 |
UT_DEBUGMSG(("Could not open key [%s]\n",szSubKey)); |
| 306 |
return X_Error; |
| 307 |
} |
| 308 |
|
| 309 |
switch (dwDisposition) |
| 310 |
{ |
| 311 |
default: |
| 312 |
UT_ASSERT(UT_SHOULD_NOT_HAPPEN); |
| 313 |
if (*pkNew) |
| 314 |
RegCloseKey(*pkNew); |
| 315 |
*pkNew = 0; |
| 316 |
return X_Error; |
| 317 |
|
| 318 |
case REG_CREATED_NEW_KEY: |
| 319 |
return X_CreatedKey; |
| 320 |
|
| 321 |
case REG_OPENED_EXISTING_KEY: |
| 322 |
return X_ExistingKey; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
void XAP_Win32Slurp::stuffRegistry(const char * szSuffix, |
| 327 |
const char * szApplicationName, |
| 328 |
LPCWSTR szExePathname, |
| 329 |
const char * szContentType) |
| 330 |
{ |
| 331 |
// load the system registry if there's no info |
| 332 |
// for us already present for the given suffix. |
| 333 |
// we assume that the string contains the dot. |
| 334 |
// |
| 335 |
// TODO consider raising a dialog asking if they |
| 336 |
// TODO want us to override any existing settings |
| 337 |
// TODO if they currently don't point to us. like |
| 338 |
// TODO MSFT and NSCP currently (politely) fight |
| 339 |
// TODO for the .html suffix.... |
| 340 |
// |
| 341 |
// we construct the following pattern in the registry: |
| 342 |
// |
| 343 |
// HKEY_CLASSES_ROOT\<suffix> = <foo> |
| 344 |
// HKEY_CLASSES_ROOT\<suffix>\Content Type = <content_type> |
| 345 |
// HKEY_CLASSES_ROOT\<foo> = <application_name> ## " Document" |
| 346 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\command = <exe_pathname> |
| 347 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\ddeexec = [Open("%1")] |
| 348 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\ddeexec\application = <application_name> |
| 349 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\ddeexec\topic = System |
| 350 |
// HKEY_CLASSES_ROOT\<foo>\DefaultIcon = <exe_pathname,2> |
| 351 |
|
| 352 |
#define VALUE_DDEEXEC_OPEN "[Open(\"%1\")]" |
| 353 |
#define FORMAT_OUR_INDIRECTION "AbiSuite.%s" |
| 354 |
#define CONTENT_TYPE_KEY "Content Type" |
| 355 |
#define DOCUMENT_ICON_POSITION 2 |
| 356 |
#define xx(s) ((LPBYTE)(s)),(strlen(s)+1) |
| 357 |
#define xxw(s) ((LPBYTE)(s)),((lstrlenW(s)+1)<<1) |
| 358 |
|
| 359 |
char buf[1024]; |
| 360 |
char bufOurFoo[1024]; |
| 361 |
WCHAR bufOurFooValue[1024]; |
| 362 |
WCHAR bufDefaultIconValue[1024]; |
| 363 |
DWORD dType; |
| 364 |
LONG eResult; |
| 365 |
ULONG len; |
| 366 |
bool bUpdateContentType; |
| 367 |
bool bCreateOrOverwrite = false; |
| 368 |
|
| 369 |
HKEY hKeyFoo = 0; |
| 370 |
HKEY hKeySuffix = 0; |
| 371 |
HKEY hKeyShell = 0; |
| 372 |
HKEY hKeyCommand = 0; |
| 373 |
HKEY hKeyOpen = 0; |
| 374 |
HKEY hKeyDdeExec = 0; |
| 375 |
HKEY hKeyApplication = 0; |
| 376 |
HKEY hKeyTopic = 0; |
| 377 |
HKEY hKeyDefaultIcon = 0; |
| 378 |
|
| 379 |
sprintf(bufOurFoo,"AbiSuite.%s",szApplicationName); |
| 380 |
strtok(bufOurFoo," "); // trim key at first whitespace |
| 381 |
|
| 382 |
const gchar *pStr = "%s Document"; |
| 383 |
UT_Win32LocaleString tmpl; |
| 384 |
char tdbuf[512]; |
| 385 |
sprintf(tdbuf,pStr,szApplicationName); |
| 386 |
tmpl.fromUTF8(tdbuf); |
| 387 |
wcscpy(bufOurFooValue,tmpl.c_str()); |
| 388 |
|
| 389 |
/////////////////////////////////////////////////////////////////// |
| 390 |
// See if someone has claimed this suffix. |
| 391 |
// HKEY_CLASSES_ROOT\<suffix> = <foo> |
| 392 |
/////////////////////////////////////////////////////////////////// |
| 393 |
|
| 394 |
switch ( _fetchKey(HKEY_CLASSES_ROOT,szSuffix,&hKeySuffix) ) |
| 395 |
{ |
| 396 |
case X_Error: |
| 397 |
goto CleanupMess; |
| 398 |
|
| 399 |
case X_CreatedKey: // we are free to create what we want. |
| 400 |
bCreateOrOverwrite = true; |
| 401 |
break; |
| 402 |
|
| 403 |
case X_ExistingKey: // see what's already there |
| 404 |
{ |
| 405 |
/////////////////////////////////////////////////////////////////// |
| 406 |
// HKEY_CLASSES_ROOT\<suffix> = <foo> |
| 407 |
// was already present. Verify the value. |
| 408 |
/////////////////////////////////////////////////////////////////// |
| 409 |
|
| 410 |
len = G_N_ELEMENTS(buf); |
| 411 |
eResult = RegQueryValueEx(hKeySuffix,NULL,NULL,&dType,(LPBYTE)buf,&len); |
| 412 |
|
| 413 |
if ((eResult != ERROR_SUCCESS) || (dType != REG_SZ) || (len==0)) |
| 414 |
break; // bogus data, overwrite it. |
| 415 |
|
| 416 |
UT_DEBUGMSG(("Registry: suffix [HKEY_CLASSES_ROOT\\%s] --> [%s]\n", |
| 417 |
szSuffix,buf)); |
| 418 |
|
| 419 |
if (g_ascii_strcasecmp(buf,bufOurFoo) != 0) // we didn't create this so ask first. |
| 420 |
{ |
| 421 |
if (!_askForStealFromAnotherApplication()) |
| 422 |
goto CleanupMess; |
| 423 |
|
| 424 |
bCreateOrOverwrite = true; |
| 425 |
break; |
| 426 |
} |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
if (bCreateOrOverwrite) |
| 431 |
{ |
| 432 |
/////////////////////////////////////////////////////////////////// |
| 433 |
// Set the value <foo> in |
| 434 |
// HKEY_CLASSES_ROOT\<suffix> = <foo> |
| 435 |
/////////////////////////////////////////////////////////////////// |
| 436 |
|
| 437 |
UT_ASSERT(hKeySuffix); |
| 438 |
eResult = RegSetValueEx(hKeySuffix,NULL,0,REG_SZ,xx(bufOurFoo)); |
| 439 |
|
| 440 |
UT_DEBUGMSG(("Register: HKEY_CLASSES_ROOT\\%s <-- %s [error %d]\n", |
| 441 |
szSuffix,bufOurFoo,eResult)); |
| 442 |
|
| 443 |
if (eResult != ERROR_SUCCESS) |
| 444 |
goto CleanupMess; |
| 445 |
} |
| 446 |
|
| 447 |
/////////////////////////////////////////////////////////////////// |
| 448 |
// See if "HKEY_CLASSES_ROOT\<suffix>\Content Type" is present |
| 449 |
// as a value under the current key. |
| 450 |
/////////////////////////////////////////////////////////////////// |
| 451 |
|
| 452 |
bUpdateContentType = true; |
| 453 |
len = G_N_ELEMENTS(buf); |
| 454 |
eResult = RegQueryValueEx(hKeySuffix,CONTENT_TYPE_KEY,NULL,&dType,(LPBYTE)buf,&len); |
| 455 |
if ((eResult == ERROR_SUCCESS) && (dType == REG_SZ)) |
| 456 |
{ |
| 457 |
UT_DEBUGMSG(("Registry: Existing ContentType [%s]\n",buf)); |
| 458 |
if (g_ascii_strcasecmp(buf,szContentType) == 0) |
| 459 |
bUpdateContentType = false; |
| 460 |
else // we didn't create this so ask first. |
| 461 |
bUpdateContentType = (_askForStealMimeFromAnotherApplication()); |
| 462 |
} |
| 463 |
|
| 464 |
if (bUpdateContentType) // bogus or stale data, overwrite it. |
| 465 |
{ |
| 466 |
/////////////////////////////////////////////////////////////////// |
| 467 |
// Set the value <content_type> in |
| 468 |
// HKEY_CLASSES_ROOT\<suffix>\Content Type = <content_type> |
| 469 |
/////////////////////////////////////////////////////////////////// |
| 470 |
|
| 471 |
UT_ASSERT(hKeySuffix); |
| 472 |
eResult = RegSetValueEx(hKeySuffix,CONTENT_TYPE_KEY,0,REG_SZ,xx(szContentType)); |
| 473 |
|
| 474 |
UT_DEBUGMSG(("Register: HKEY_CLASSES_ROOT\\%s\\Content Type <-- %s [error %d]\n", |
| 475 |
szSuffix,szContentType,eResult)); |
| 476 |
|
| 477 |
// content-type is not a critical field, so if it fails we just go on. |
| 478 |
// if (eResult != ERROR_SUCCESS) goto CleanupMess; |
| 479 |
} |
| 480 |
|
| 481 |
/////////////////////////////////////////////////////////////////// |
| 482 |
// Verify that the suffix indirection is defined. |
| 483 |
// HKEY_CLASSES_ROOT\<foo> = ... |
| 484 |
/////////////////////////////////////////////////////////////////// |
| 485 |
|
| 486 |
switch ( _fetchKey(HKEY_CLASSES_ROOT,bufOurFoo,&hKeyFoo) ) |
| 487 |
{ |
| 488 |
case X_Error: |
| 489 |
goto CleanupMess; |
| 490 |
|
| 491 |
case X_ExistingKey: |
| 492 |
UT_ASSERT(hKeyFoo); |
| 493 |
len = G_N_ELEMENTS(buf); |
| 494 |
eResult = RegQueryValueExW(hKeyFoo,NULL,0,&dType,(LPBYTE)buf,&len); |
| 495 |
if ((eResult==ERROR_SUCCESS) && (dType==REG_SZ) && (lstrcmpiW((LPCWSTR)buf,bufOurFooValue)==0)) |
| 496 |
break; // already has correct value, no need to overwrite. |
| 497 |
|
| 498 |
/* otherwise, replace the value */ |
| 499 |
/* fall thru intended */ |
| 500 |
|
| 501 |
case X_CreatedKey: |
| 502 |
UT_ASSERT(hKeyFoo); |
| 503 |
eResult = RegSetValueExW(hKeyFoo,NULL,0,REG_SZ,xxw(bufOurFooValue)); |
| 504 |
if (eResult != ERROR_SUCCESS) |
| 505 |
goto CleanupMess; |
| 506 |
break; |
| 507 |
} |
| 508 |
|
| 509 |
/////////////////////////////////////////////////////////////////// |
| 510 |
// Inspect the command path |
| 511 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\command = <exe_pathname> |
| 512 |
/////////////////////////////////////////////////////////////////// |
| 513 |
WCHAR commandPathWithParam[1024]; |
| 514 |
wcscpy ( commandPathWithParam, szExePathname ); |
| 515 |
wcscat ( commandPathWithParam, L" \"%1\"" ); |
| 516 |
|
| 517 |
if (_fetchKey(hKeyFoo,"shell",&hKeyShell) == X_Error) |
| 518 |
goto CleanupMess; |
| 519 |
if (_fetchKey(hKeyShell,"open",&hKeyOpen) == X_Error) |
| 520 |
goto CleanupMess; |
| 521 |
switch ( _fetchKey(hKeyOpen,"command",&hKeyCommand) ) |
| 522 |
{ |
| 523 |
case X_Error: |
| 524 |
goto CleanupMess; |
| 525 |
|
| 526 |
case X_ExistingKey: |
| 527 |
UT_ASSERT(hKeyCommand); |
| 528 |
len = G_N_ELEMENTS((LPWSTR)buf); |
| 529 |
eResult = RegQueryValueExW(hKeyCommand,NULL,0,&dType,(LPBYTE)buf,&len); |
| 530 |
if ((eResult==ERROR_SUCCESS) && (dType==REG_SZ)) |
| 531 |
{ |
| 532 |
if (lstrcmpiW((LPCWSTR)buf,commandPathWithParam) == 0) |
| 533 |
break; // already has correct value, no need to overwrite. |
| 534 |
|
| 535 |
if(memcmp(buf, commandPathWithParam, lstrlenW(commandPathWithParam)<<1) == 0) |
| 536 |
{ |
| 537 |
// Path name is the same but has extra at the end. |
| 538 |
// Probably "%1" |
| 539 |
|
| 540 |
// Fall throught to update path name. |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
|
| 545 |
if (!_askForUpdateExePathname()) |
| 546 |
goto CleanupMess; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
/* otherwise, replace the value */ |
| 551 |
/* fall thru intended */ |
| 552 |
|
| 553 |
case X_CreatedKey: |
| 554 |
UT_ASSERT(hKeyCommand); |
| 555 |
eResult = RegSetValueExW(hKeyCommand,NULL,0,REG_SZ,xxw(commandPathWithParam)); |
| 556 |
if (eResult != ERROR_SUCCESS) |
| 557 |
goto CleanupMess; |
| 558 |
break; |
| 559 |
} |
| 560 |
|
| 561 |
/////////////////////////////////////////////////////////////////// |
| 562 |
// Inspect the ddeexec key |
| 563 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\ddeexec = [Open(%1)] |
| 564 |
/////////////////////////////////////////////////////////////////// |
| 565 |
|
| 566 |
switch ( _fetchKey(hKeyOpen,"ddeexec",&hKeyDdeExec) ) |
| 567 |
{ |
| 568 |
case X_Error: |
| 569 |
goto CleanupMess; |
| 570 |
|
| 571 |
case X_ExistingKey: |
| 572 |
UT_ASSERT(hKeyDdeExec); |
| 573 |
len = G_N_ELEMENTS(buf); |
| 574 |
eResult = RegQueryValueEx(hKeyDdeExec,NULL,0,&dType,(LPBYTE)buf,&len); |
| 575 |
if ((eResult==ERROR_SUCCESS) && (dType==REG_SZ) && (g_ascii_strcasecmp(buf,VALUE_DDEEXEC_OPEN)==0)) |
| 576 |
break; // already has correct value, no need to overwrite. |
| 577 |
|
| 578 |
/* otherwise, replace the value */ |
| 579 |
/* fall thru intended */ |
| 580 |
|
| 581 |
case X_CreatedKey: |
| 582 |
UT_ASSERT(hKeyDdeExec); |
| 583 |
eResult = RegSetValueEx(hKeyDdeExec,NULL,0,REG_SZ,xx(VALUE_DDEEXEC_OPEN)); |
| 584 |
if (eResult != ERROR_SUCCESS) |
| 585 |
goto CleanupMess; |
| 586 |
break; |
| 587 |
} |
| 588 |
|
| 589 |
/////////////////////////////////////////////////////////////////// |
| 590 |
// Inspect the application key |
| 591 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\ddeexec\application = <application_name> |
| 592 |
/////////////////////////////////////////////////////////////////// |
| 593 |
|
| 594 |
switch ( _fetchKey(hKeyDdeExec,"application",&hKeyApplication) ) |
| 595 |
{ |
| 596 |
case X_Error: |
| 597 |
goto CleanupMess; |
| 598 |
|
| 599 |
case X_ExistingKey: |
| 600 |
UT_ASSERT(hKeyApplication); |
| 601 |
len = G_N_ELEMENTS(buf); |
| 602 |
eResult = RegQueryValueEx(hKeyApplication,NULL,0,&dType,(LPBYTE)buf,&len); |
| 603 |
if ((eResult==ERROR_SUCCESS) && (dType==REG_SZ) && (g_ascii_strcasecmp(buf,szApplicationName)==0)) |
| 604 |
break; // already has correct value, no need to overwrite. |
| 605 |
|
| 606 |
/* otherwise, replace the value */ |
| 607 |
/* fall thru intended */ |
| 608 |
|
| 609 |
case X_CreatedKey: |
| 610 |
UT_ASSERT(hKeyApplication); |
| 611 |
eResult = RegSetValueEx(hKeyApplication,NULL,0,REG_SZ,xx(szApplicationName)); |
| 612 |
if (eResult != ERROR_SUCCESS) |
| 613 |
goto CleanupMess; |
| 614 |
break; |
| 615 |
} |
| 616 |
|
| 617 |
/////////////////////////////////////////////////////////////////// |
| 618 |
// Inspect the topic key |
| 619 |
// HKEY_CLASSES_ROOT\<foo>\shell\open\ddeexec\topic = System |
| 620 |
/////////////////////////////////////////////////////////////////// |
| 621 |
|
| 622 |
switch ( _fetchKey(hKeyDdeExec,"topic",&hKeyTopic) ) |
| 623 |
{ |
| 624 |
case X_Error: |
| 625 |
goto CleanupMess; |
| 626 |
|
| 627 |
case X_ExistingKey: |
| 628 |
UT_ASSERT(hKeyTopic); |
| 629 |
len = G_N_ELEMENTS((LPWSTR)buf); |
| 630 |
eResult = RegQueryValueExW(hKeyTopic,NULL,0,&dType,(LPBYTE)buf,&len); |
| 631 |
if ((eResult==ERROR_SUCCESS) && (dType==REG_SZ) && (wcsicmp((LPWSTR)buf,MY_DDE_TOPICNAME)==0)) |
| 632 |
break; // already has correct value, no need to overwrite. |
| 633 |
|
| 634 |
/* otherwise, replace the value */ |
| 635 |
/* fall thru intended */ |
| 636 |
|
| 637 |
case X_CreatedKey: |
| 638 |
UT_ASSERT(hKeyTopic); |
| 639 |
eResult = RegSetValueExW(hKeyTopic,NULL,0,REG_SZ,xxw(MY_DDE_TOPICNAME)); |
| 640 |
if (eResult != ERROR_SUCCESS) |
| 641 |
goto CleanupMess; |
| 642 |
break; |
| 643 |
} |
| 644 |
|
| 645 |
/////////////////////////////////////////////////////////////////// |
| 646 |
// Set the default icon for the suffix (this is for Explorer et al. |
| 647 |
// HKEY_CLASSES_ROOT\<foo>\DefaultIcon = <exe_pathname,2> |
| 648 |
/////////////////////////////////////////////////////////////////// |
| 649 |
|
| 650 |
wsprintfW(bufDefaultIconValue,L"%s,%d",szExePathname,DOCUMENT_ICON_POSITION); |
| 651 |
switch ( _fetchKey(hKeyFoo,"DefaultIcon",&hKeyDefaultIcon) ) |
| 652 |
{ |
| 653 |
case X_Error: |
| 654 |
goto CleanupMess; |
| 655 |
|
| 656 |
case X_ExistingKey: |
| 657 |
UT_ASSERT(hKeyDefaultIcon); |
| 658 |
len = G_N_ELEMENTS((LPWSTR)buf); |
| 659 |
eResult = RegQueryValueExW(hKeyDefaultIcon,NULL,0,&dType,(LPBYTE)buf,&len); |
| 660 |
if ((eResult==ERROR_SUCCESS) && (dType==REG_SZ) && (lstrcmpiW((LPWSTR)buf,bufDefaultIconValue)==0)) |
| 661 |
break; // already has correct value, no need to overwrite. |
| 662 |
|
| 663 |
/* otherwise, replace the value */ |
| 664 |
/* fall thru intended */ |
| 665 |
|
| 666 |
case X_CreatedKey: |
| 667 |
UT_ASSERT(hKeyDefaultIcon); |
| 668 |
eResult = RegSetValueExW(hKeyDefaultIcon,NULL,0,REG_SZ,xxw(bufDefaultIconValue)); |
| 669 |
if (eResult != ERROR_SUCCESS) |
| 670 |
goto CleanupMess; |
| 671 |
break; |
| 672 |
} |
| 673 |
|
| 674 |
/////////////////////////////////////////////////////////////////// |
| 675 |
// Success. |
| 676 |
/////////////////////////////////////////////////////////////////// |
| 677 |
|
| 678 |
UT_DEBUGMSG(("Successfully stuffed the registry for suffix [%s].\n",szSuffix)); |
| 679 |
|
| 680 |
CleanupMess: |
| 681 |
|
| 682 |
#define KILLKEY(k) do { if (k) RegCloseKey(k); (k) = 0; } while (0) |
| 683 |
|
| 684 |
KILLKEY(hKeyFoo); |
| 685 |
KILLKEY(hKeySuffix); |
| 686 |
KILLKEY(hKeyShell); |
| 687 |
KILLKEY(hKeyCommand); |
| 688 |
KILLKEY(hKeyOpen); |
| 689 |
KILLKEY(hKeyDdeExec); |
| 690 |
KILLKEY(hKeyApplication); |
| 691 |
KILLKEY(hKeyTopic); |
| 692 |
KILLKEY(hKeyDefaultIcon); |
| 693 |
|
| 694 |
return; |
| 695 |
} |
| 696 |
|
| 697 |
bool XAP_Win32Slurp::_askForStealFromAnotherApplication(void) const |
| 698 |
{ |
| 699 |
// TODO install a real dialog that asks the user and |
| 700 |
// TODO allows us to set preference value for never |
| 701 |
// TODO asking again or always steal or whatever... |
| 702 |
|
| 703 |
UT_DEBUGMSG(("Registry: Suffix not ours.\n")); |
| 704 |
bool bResult = false; |
| 705 |
|
| 706 |
#ifdef DEBUG |
| 707 |
bResult = true; |
| 708 |
#endif |
| 709 |
|
| 710 |
return bResult; |
| 711 |
} |
| 712 |
|
| 713 |
bool XAP_Win32Slurp::_askForStealMimeFromAnotherApplication(void) const |
| 714 |
{ |
| 715 |
// TODO install a real dialog that asks the user and |
| 716 |
// TODO allows us to set preference value for never |
| 717 |
// TODO asking again or always steal or whatever... |
| 718 |
|
| 719 |
UT_DEBUGMSG(("Registry: MIME type not ours.\n")); |
| 720 |
bool bResult = false; |
| 721 |
|
| 722 |
#ifdef DEBUG |
| 723 |
bResult = true; |
| 724 |
#endif |
| 725 |
|
| 726 |
return bResult; |
| 727 |
} |
| 728 |
|
| 729 |
bool XAP_Win32Slurp::_askForUpdateExePathname(void) const |
| 730 |
{ |
| 731 |
// TODO install a real dialog that asks the user if |
| 732 |
// TODO we want to change the pathname of the exe in |
| 733 |
// TODO the association and to set preference values |
| 734 |
// TODO to never asking again or always set it or |
| 735 |
// TODO whatever... |
| 736 |
|
| 737 |
UT_DEBUGMSG(("Registry: Need to update EXE pathname...\n")); |
| 738 |
bool bResult = false; |
| 739 |
|
| 740 |
return bResult; |
| 741 |
} |
| 742 |
|