-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktop.py
66 lines (43 loc) · 1.55 KB
/
desktop.py
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
import re, os, glob, subprocess, ConfigParser
from WebkitQObject import WebkitQObject
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
class DesktopLauncher(QObject):
def __init__(self, path):
super(DesktopLauncher, self).__init__()
self.path = path
self.config = ConfigParser.SafeConfigParser()
self.config.read(self.path)
self.icon = QIcon.fromTheme(self.config.get('Desktop Entry', 'Icon'))
@pyqtSlot(int, result=str)
def icon(self, size=64):
size = QSize(size, size)
pixmap = self.icon.pixmap(size * 1.1).scaled(size, transformMode=Qt.SmoothTransformation)
byte_array = QByteArray()
buffer = QBuffer(byte_array)
buffer.open(QIODevice.WriteOnly)
pixmap.save(buffer, 'PNG')
return 'data:image/png;base64,' + str(byte_array.toBase64())
@pyqtSlot()
def launch(self):
with open(self.path, 'r') as handle:
try:
command = re.search(r'\bExec=(.*?)\n', handle.read()).group(1)
except AttributeError:
return False
return subprocess.call(command, shell=True)
@pyqtProperty(str)
def name(self):
return self.config.get('Desktop Entry', 'Name')
class Desktop(WebkitQObject):
def __init__(self, path='~/Desktop'):
super(Desktop, self).__init__()
self.path = os.path.expanduser(path)
@pyqtProperty(QVariant)
def launchers(self):
launchers = []
for path in sorted(glob.glob(os.path.join(self.path, '*.desktop'))):
launcher = DesktopLauncher(path)
launchers.append(launcher)
return self.store(launchers)