summaryrefslogtreecommitdiffstats
path: root/wineconfig/wineread.py
blob: c1fa3c5c7d3be8c8df38e9dae873bcf10c0a6ee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#!/usr/bin/python
# -*- coding: UTF-8 -*-
###########################################################################
# wineread.py - description                                             #
# ------------------------------                                          #
# begin     : Fri Mar 26 2004                                             #
# copyright : (C) 2006 by Yuriy Kozlov                                    #
# email     : [email protected]                                      #
#                                                                         #
###########################################################################
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
###########################################################################

import os

""" Provides a set of functions for accessing wine's settings """

# Assumes the fake windows is installed in ~/.wine
default_winepath = os.environ['HOME'] + "/.wine"
winepath = default_winepath
defaultwinfolderspath = "c:\\windows\\profiles\\" + os.environ['USER']

# Where the dll's are
default_winebuildpath = "/usr/lib/wine"
default_winebuildpath2 = "/usr/lib32/wine"
default_winebuildpath3 = "/usr/local/lib/wine"
    
winebuildpath = None

def GetWineBuildPath():
    """ Returns the wine build path, None if not present """
    path = None
    if winebuildpath:
        path = winebuildpath
    elif os.path.exists(default_winebuildpath):
        path = default_winebuildpath
    elif os.path.exists(default_winebuildpath2):
        path = default_winebuildpath2
    elif os.path.exists(default_winebuildpath3):
        path = default_winebuildpath3

    return path
    
def SetWineBuildPath(path):
    """ Sets the winebuildpath to path """
    global winebuildpath
    winebuildpath = path

# drive = [index, letter, unixpath/mapping, type, label, serial]
empty_drives = (
    [0,"A:","","","",""],
    [1,"B:","","","",""],
    [2,"C:","","","",""],
    [3,"D:","","","",""],
    [4,"E:","","","",""],
    [5,"F:","","","",""],
    [6,"G:","","","",""],
    [7,"H:","","","",""],
    [8,"I:","","","",""],
    [9,"J:","","","",""],
    [10,"K:","","","",""],
    [11,"L:","","","",""],
    [12,"M:","","","",""],
    [13,"N:","","","",""],
    [14,"O:","","","",""],
    [15,"P:","","","",""],
    [16,"Q:","","","",""],
    [17,"R:","","","",""],
    [18,"S:","","","",""],
    [19,"T:","","","",""],
    [20,"U:","","","",""],
    [21,"V:","","","",""],
    [22,"W:","","","",""],
    [23,"X:","","","",""],
    [24,"Y:","","","",""],
    [25,"Z:","","","",""])
     
def GetEmptyDrives():
    """ Returns a list of 26 empty drives """
    drives = []
    for drive in empty_drives:
        drives.append(drive[:])
    return drives
        
def LoadDrives():
    drives = GetEmptyDrives()
    
    driveletters = os.listdir(winepath + "/dosdevices")
    for folder in driveletters:
        if len(folder) > 2 or folder[1] != ':':
            del folder
    set(driveletters)
    
    drivetypes = GetDriveTypes()
    
    for drive in drives:
        letter = drive[1].lower()
        if letter in driveletters:
            drive[2] = os.readlink(winepath + "/dosdevices/" + letter)
            if drivetypes.has_key(drive[1]):
                drive[3] = drivetypes[drive[1]]
    return drives

empty_shelllinks = ([26,"Desktop","","","",""],
    [27,"My Documents","","","",""],
    [28,"My Pictures","","","",""],
    [29,"My Music","","","",""],
    [30,"My Video","","","",""])
    
folder_nonexistent = "This folder does not exist, please map it."
profilesdirectory = winepath + "/dosdevices/c:/windows/profiles/" + os.environ['USER']
        
def GetEmptyShellLinks():
    """ Returns a list of important windows folders """
    shelllinks = []
    for link in empty_shelllinks:
        shelllinks.append(link[:])
    return shelllinks
        
def GetShellLinks():
    shelllinks = GetEmptyShellLinks()
        
    existingshelllinks = os.listdir(profilesdirectory)
    set(existingshelllinks)
    shellregistry = GetShellRegistry()
    usershellregistry = GetUserShellRegistry()
    
    for link in shelllinks:
        if link[1] in existingshelllinks:
            linkpath = profilesdirectory + "/" + link[1]
            if os.path.islink(linkpath):
                link[2] = os.readlink(linkpath)
            else:
                link[2] = linkpath
            link[3] = "shellfolder"
            winpath = defaultwinfolderspath + "\\" + link[1]
            link[4] = winpath
            link[5] = shellregistry.get(link[1], defaultwinfolderspath + "\\" + link[1])
            link[5] = link[5].replace("\\\\","\\")
        else:
            link[3] = "shellfolder"
            link[4] = folder_nonexistent
            link[5] = shellregistry.get(link[1], folder_nonexistent)
            link[5] = link[5].replace("\\\\","\\")
            
    return shelllinks

def GetValue(key, value):
    """ Returns a specific value, returns a blank string if the value is not there. """
    # Need 4 \'s to generate one because both python and the shell use it as an escape character
    key = key.replace("\\","\\\\")
    key = key.replace(" ","\\ ")
    error = os.system("wine regedit /E .registryvalue.reg " + key)
    if error != 0:
        return ""
    
    file=open('.registryvalue.reg')
    
    for line in file:
        if line and line[0] == '"' or line[0] == '@':
            line = line.strip('\r\n')
            line = line.split('=')
            line = (line[0].strip('"'),line[1].strip('"@'))
            if line[0] == value:
                file.close()
                os.remove(".registryvalue.reg")
                return line[1]
    else:
        file.close()
        os.remove(".registryvalue.reg")
        return ""

def GetKeyValues(key):
    """ Returns a dictionary of all the values in the key
    Returns an empty dictionary if the key does not exist
    Does not read subkeys within the key """
    # Need 4 \'s to generate one because both python and the shell use it as an escape character
    key = key.replace("\\","\\\\")
    key = key.replace(" ","\\ ")
    error = os.system("wine regedit /E .registrykey.reg " + key)
    if error != 0:
        return {}
    
    settings = {}
    
    file=open('.registrykey.reg')
    
    keycount = 0
    for line in file:
        if keycount > 1:
            break
        elif line and line[0] == '[':
            keycount += 1
        elif line and line[0] == '"':
            line = line.split('=')
            settings[line[0].strip('"')] = line[1].strip('"\r\n@')
    
    file.close()
    os.remove(".registrykey.reg")
        
    return settings

def GetUserShellRegistry():
    error = os.system("wine regedit /E .registryshelluser.reg HKEY_USERS\\\\.Default\\\\Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Explorer\\\\User\\ Shell\\ Folders")
    if error != 0:
        return {}
    
    usershellfile=open('.registryshelluser.reg')
    usershellfilelines = usershellfile.readlines()
    usershellfile.close()
    os.remove(".registryshelluser.reg")
    
    settings = {}
    
    del(usershellfilelines[:3])
    
    for usershellline in usershellfilelines:
        usershellline = usershellline.split('=')
        settings[usershellline[0].strip('"')] = usershellline[1].strip('"\r\n')
        
    return settings
    
def GetShellRegistry():
    error = os.system("wine regedit /E .registryshell.reg HKEY_USERS\\\\.Default\\\\Software\\\\Microsoft\\\\Windows\\\\CurrentVersion\\\\Explorer\\\\Shell\\ Folders")
    if error != 0:
        return {}
    
    shellfile=open('.registryshell.reg')
    shellfilelines = shellfile.readlines()
    shellfile.close()
    os.remove(".registryshell.reg")
    
    settings = {}
    
    del(shellfilelines[:3])
    
    for shellline in shellfilelines:
        shellline = shellline.split('=')
        settings[shellline[0].strip('"')] = shellline[1].strip('"\r\n')
        
    return settings
    
def GetDriveTypes():
    """ Returns a dictionary of the drive types """
    # Drive C: doesn't get stored in the registry
    defaulttypes = {"C:":"hd"}
    
    types = GetKeyValues("HKEY_LOCAL_MACHINE\\Software\\Wine\\Drives")
    types.update(defaulttypes)
    
    return types
    
def GetAudioDriver():
    """ Returns the audio driver currently set in the registry """
    return GetValue("HKEY_CURRENT_USER\\Software\\Wine\\Drivers","Audio")
    
def GetDSoundSettings(app = None):
    """ Returns a dictionary of the settings for the DirectSound section """
    if not app:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\DirectSound")
    else:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\AppDefaults\\" +\
            app + "\\DirectSound")
    
def GetWindowSettings(app = None):
    """ Returns a dictionary of the Window Settings """
    if not app:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\X11 Driver")
    else:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\AppDefaults\\" +\
            app + "\\X11 Driver")
    
def GetD3DSettings(app = None):
    """ Returns a dictionary of the Direct3D Settings """
    if not app:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\Direct3D")
    else:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\AppDefaults\\" +\
            app + "\\Direct3D")

# Copied from winecfg
winversions = (
        ( "win2003", "Windows 2003",   5,  2, 0xECE, "VER_PLATFORM_WIN32_NT", "Service Pack 1", 1, 0, "ServerNT"),
        ( "winxp",   "Windows XP",     5,  1, 0xA28, "VER_PLATFORM_WIN32_NT", "Service Pack 2", 2, 0, "WinNT"),
        ( "win2k",   "Windows 2000",   5,  0, 0x893, "VER_PLATFORM_WIN32_NT", "Service Pack 4", 4, 0, "WinNT"),
        ( "winme",   "Windows ME",     4, 90, 0xBB8, "VER_PLATFORM_WIN32_WINDOWS", " ", 0, 0, ""),
        ( "win98",   "Windows 98",     4, 10, 0x8AE, "VER_PLATFORM_WIN32_WINDOWS", " A ", 0, 0, ""),
        ( "win95",   "Windows 95",     4,  0, 0x3B6, "VER_PLATFORM_WIN32_WINDOWS", "", 0, 0, ""),
        ( "nt40",    "Windows NT 4.0", 4,  0, 0x565, "VER_PLATFORM_WIN32_NT", "Service Pack 6a", 6, 0, "WinNT"),
        ( "nt351",   "Windows NT 3.5", 3, 51, 0x421, "VER_PLATFORM_WIN32_NT", "Service Pack 2", 0, 0, "WinNT"),
        ( "win31",   "Windows 3.1",    2, 10,     0, "VER_PLATFORM_WIN32s", "Win32s 1.3", 0, 0, ""),
        ( "win30",   "Windows 3.0",    3,  0,     0, "VER_PLATFORM_WIN32s", "Win32s 1.3", 0, 0, ""),
        ( "win20",   "Windows 2.0",    2,  0,     0, "VER_PLATFORM_WIN32s", "Win32s 1.3", 0, 0, ""))
    
def GetGeneralWineSettings(app = None):
    """ Returns a dictionary of the general wine Settings, including the windows version """
    if not app:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine")
    else:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\AppDefaults\\" +\
            app)
    
def GetApps():
    """
    Returns a list of the applications which have keys for application
    specific settings.
    """
    error = os.system("wine regedit /E .registryapps.reg HKEY_CURRENT_USER\\\\Software\\\\Wine\\\\AppDefaults")
    if error != 0:
        return []
    
    settingsfile=open('.registryapps.reg')
    settingsfilelines = settingsfile.readlines()
    settingsfile.close()
    os.remove('.registryapps.reg')
    
    apps = set([])
    
    del(settingsfilelines[:3])
    
    for line in settingsfilelines:
        if line[0] == '[':
            line = line.split('\\')
            line[4] = line[4].strip(']\r\n')
            apps.add(line[4])
            
    apps = list(apps)
    apps.sort()
    
    return apps
    
builtin_only = set(("advapi32",
    "capi2032",
    "dbghelp",
    "ddraw",
    "gdi32",
    "glu32",
    "icmp",
    "iphlpapi",
    "joystick.drv",
    "kernel32",
    "mswsock",
    "ntdll",
    "opengl32",
    "stdole2.tlb",
    "stdole32.tlb",
    "twain_32",
    "unicows",
    "user32",
    "vdmdbg",
    "w32skrnl",
    "winealsa.drv",
    "winearts.drv",
    "wineaudioio.drv",
    "wined3d",
    "winedos",
    "wineesd.drv",
    "winejack.drv",
    "winemp3.acm",
    "winenas.drv",
    "wineoss.drv",
    "wineps",
    "wineps.drv",
    "winex11.drv",
    "winmm",
    "wintab32",
    "wnaspi32",
    "wow32",
    "ws2_32",
    "wsock32"))

def GetDllsList():
    """ Returns a list of dlls that can be overridden """
    origdlls = os.listdir(GetWineBuildPath())
    dlls = [""]
    
    for dll in origdlls:
        dll = dll.rstrip('.so')
        dots = dll.count('.')
        if dots != 1:
            continue
        dll, extension = dll.split('.')
        if not (extension != "dll" or dll in builtin_only):
            dlls.append(dll)
            
    dlls.sort()
    return dlls
        
def GetDllOverrides(app = None):
    """ Returns a dictionary of overridden dlls """
    if not app:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\DllOverrides")
    else:
        return GetKeyValues("HKEY_CURRENT_USER\\Software\\Wine\\AppDefaults\\" +\
            app + "\\DllOverrides")
            
            
# --- Getting and Setting the Default browser ---

# WineBrowser>Browsers
# List of browsers that Wine will attempt to launch when running winebrowser
# command or clicking on a link in a windows application. Default value is
default_browserlist = ["firefox","konqueror","mozilla","netscape","galeon","opera","dillo"]

# WineBrowser>Mailers
# List of mail clients that Wine will attempt to launch when running winebrowser
# Default value is 
default_mailerlist = ["mozilla-thunderbird","thunderbird","evolution","kmail"]
      
#with firefox installed
browser_formats = ["CHROME","FirefoxHTML","HTML","htmlfile","FTP","GOPHER","http","https"]

default_browser_formats = ["htmlfile","http","https"]   # just "winebrowser"
      
default_mailer_formats = ["mailto"]     # "winebrowser %1"

def GetBrowser():
    """ Returns the default browser """
    browser = GetValue("HKEY_LOCAL_MACHINE\\Software\\Classes\\http\\shell\\open\\command",'@')
    
    if browser == "winebrowser":
        return GetWineBrowser()
    else:
        return browser
    
def GetWineBrowser():
    """ Returns the first browser tried by winebrowser """
    browserlist = GetValue("HKEY_CURRENT_USER\\Software\\Wine\\WineBrowser","Browsers")
    if browserlist:
        browser = browserlist.split(',')[0].strip()
        return browser
    else:
        return default_browserlist[0]
    
#def GetWinBrowserList():
    
def GetNativeBrowserList():
    """ Returns the list of browsers tried by winebrowser """
    browserlist = GetValue("HKEY_CURRENT_USER\\Software\\Wine\\WineBrowser","Browsers")
    if browserlist:
        browserlist = list(set(browserlist.split(',')))
        for i,item in enumerate(browserlist):
            browserlist[i] = item.strip()
        return browserlist
    else:
        return default_browserlist
    
def GetMailer():
    """ Returns the default mail client """
    mailer = GetValue("HKEY_LOCAL_MACHINE\\Software\\Classes\\mailto\\shell\\open\\command",'@')
    
    if mailer == "winebrowser" or mailer == "winebrowser %1":
        return GetWineMailer()
    else:
        return mailer
    
def GetWineMailer():
    """ Returns the first mail client tried by winebrowser """
    mailerlist = GetValue("HKEY_CURRENT_USER\\Software\\Wine\\WineBrowser","Mailers")
    if mailerlist:
        mailer = mailerlist.split(',')[0].strip()
        return mailer
    else:
        # Default first mailer to try in wine is mozilla-thunderbird
        return default_mailerlist[0]
    
def GetNativeMailerList():
    """ Returns the list of mail clients tried by winebrowser """
    mailerlist = GetValue("HKEY_CURRENT_USER\\Software\\Wine\\WineBrowser","Mailers")
    if mailerlist:
        mailerlist = list(set(mailerlist.split(',')))
        for i,item in enumerate(mailerlist):
            mailerlist[i] = item.strip()
        return mailerlist
    else:
        return default_mailerlist


# ----- Theming -----

def GetThemesList():
    """ Returns a list of installed thiemes """
    if not os.path.exists(winepath + "/dosdevices/c:/windows/Resources/Themes"):
        return []
    origthemes = os.listdir(winepath + "/dosdevices/c:/windows/Resources/Themes")
    themes = []
    
    for i,theme in enumerate(origthemes):
        if os.path.exists(winepath +\
            "/dosdevices/c:/windows/Resources/Themes/" + theme +\
            "/" + theme + ".msstyles"):
            themes.append(theme)
            
    themes.sort()
    return themes
            
def GetCurrentTheme():
    """ Returns the current (theme,color,size), None if none is set """
    themeinfo = GetKeyValues("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\ThemeManager")
    
    if not themeinfo or themeinfo["ThemeActive"] == "0":
        return None
    else:
        # themename from themename.msstyles
        themename = themeinfo["DllName"].split('\\\\')[-1].split('.')[0]
        theme = (themename, themeinfo["ColorName"], themeinfo["SizeName"])
        return theme
    
def GetColorSettings():
    """ Returns a dictionary of the set colors """
    return GetKeyValues("HKEY_CURRENT_USER\\Control Panel\\Colors")

def GetWindowMetrics():
    """ Returns a dictionary of the WindowMetrics settings """
    return GetKeyValues("HKEY_CURRENT_USER\\Control Panel\\Desktop\\WindowMetrics")

def GetDesktopSettings():
    """ Returns a dictionary of the control panel \ Desktop settings """
    return GetKeyValues("HKEY_CURRENT_USER\\Control Panel\\Desktop")
    
def SetWinePath(path):
    """ Sets the winepath to path """
    global winepath
    winepath = path
    
def GetWinePath():
    return winepath
    
def VerifyWineDrive(path = None):
    """ Does a very basic check of if the given path is a valid fake windows installation
    Returns False if there is no C: drive """
    if not path:
        path = self.default_winepath
    
    return os.path.exists(path + "/dosdevices/c:/windows/profiles/" + os.environ['USER']) and \
        os.path.exists(path + "/dosdevices/c:/windows/system32") and \
        os.path.exists(path + "/system.reg") and os.path.exists(path + "/userdef.reg") and \
        os.path.exists(path + "/user.reg")