-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlacesIconChooser.py
executable file
·186 lines (141 loc) · 6.25 KB
/
PlacesIconChooser.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
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
#!/usr/bin/env python3
"""
Author: Denys Madureira
Change icon of selected folder
DateTimeOriginal tag
"""
from os import listdir, path
from os.path import isfile, join
import gi
import glob
import sys
import subprocess
import os
import pathlib
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gio
from gi.repository.GdkPixbuf import Pixbuf
iconTheme = subprocess.getoutput('gsettings get org.gnome.desktop.interface icon-theme')
iconTheme = iconTheme.replace('\'', '')
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Icon Chooser")
self.set_default_size(835, 400)
self.grid = Gtk.Grid(column_homogeneous=True)
self.add(self.grid)
self.scrolledwindow = Gtk.ScrolledWindow()
self.entry = Gtk.Entry()
self.entry.set_placeholder_text('Search icon')
self.entry.set_text("")
self.entry.connect("activate", self.search)
self.liststore = Gtk.ListStore(Pixbuf, str)
self.iconview = Gtk.IconView.new()
self.iconview.set_model(self.liststore)
self.iconview.set_pixbuf_column(0)
self.iconview.set_text_column(1)
self.iconview.connect("activate-cursor-item", self.apply, None)
self.iconview.connect("item-activated", self.apply)
self.filteredIcons = []
self.icons = self.getPlaceIcons()
self.getIcons(self.icons)
self.button = Gtk.Button.new_with_label("Search")
self.button.connect("clicked", self.search)
self.revertButton = Gtk.Button.new_with_label("Revert")
self.revertButton.connect("clicked", self.revert)
self.applyButton = Gtk.Button.new_with_label("Apply")
self.applyButton.connect("clicked", self.apply, None)
self.scrolledwindow.add(self.iconview)
self.scrolledwindow.set_hexpand(True)
self.scrolledwindow.set_vexpand(True)
self.grid.add(self.entry)
self.grid.attach(self.button, 0, 1, 1, 1)
self.grid.attach(self.scrolledwindow, 0, 2, 1, 1)
# self.grid.attach(self.revertButton, 0, 3, 1, 1)
self.grid.attach(self.applyButton, 0, 4, 1, 1)
def search(self, button):
value = self.entry.get_text()
def filterIcons(icons):
if(value in icons["name"]):
return True
else:
return False
filteredIcons = list(filter(filterIcons, self.icons))
self.liststore.clear()
self.getIcons(filteredIcons)
def revert(self, button):
current = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI", '').replace("file://", "").replace("%20", " ")
root = os.path.realpath(current)
click = os.getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS','')
folder = os.path.join(click)
for path in os.getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS','').splitlines():
f = Gio.File.parse_name(os.path.abspath(path))
f.set_attribute_string("metadata::custom-icon", '', Gio.FileQueryInfoFlags.NONE, None)
# subprocess.Popen(['gvfs-set-attribute', folder, '-t', 'unset', 'metadata::custom-icon'])
# os.system('gvfs-set-attribute ' + folder + ' -t unset metadata::custom-icon')
self.destroy()
def apply(self, button, param):
current = os.getenv("NAUTILUS_SCRIPT_CURRENT_URI", '').replace("file://", "").replace("%20", " ")
root = os.path.realpath(current)
click = os.getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS','')
index = int(str(self.iconview.get_selected_items()[0]))
icon = self.filteredIcons[index]
folder = os.path.join(click)
newList = [column for column in [row for row in self.liststore]]
for path in os.getenv('NAUTILUS_SCRIPT_SELECTED_FILE_PATHS','').splitlines():
f = Gio.File.parse_name(os.path.abspath(path))
f.set_attribute_string("metadata::custom-icon", "file://" + icon["path"], Gio.FileQueryInfoFlags.NONE, None)
subprocess.Popen(['nautilus', root])
self.destroy()
def getIcons(self, icons):
self.filteredIcons = []
for icon in icons:
self.filteredIcons.append(icon)
try:
pixbuf = Gtk.IconTheme.get_default().load_icon(icon["name"], 64, 0)
self.liststore.append([pixbuf, icon["name"]])
except Exception as inst:
print(inst)
def getPlaceIcons(self):
defaultPath = '/usr/share/icons/'
currentPath = ''
allFiles = []
directories = [f for f in listdir(defaultPath)]
for directory in directories:
if directory == iconTheme:
currentPath = defaultPath + directory
if path.exists(currentPath + '/48'):
currentPath = currentPath + '/48'
elif path.exists(currentPath + '/48x48'):
currentPath = currentPath + '/48x48'
elif path.exists(currentPath + '/scalable'):
currentPath = currentPath + '/scalable'
if path.exists(currentPath + '/places'):
currentPath = currentPath + '/places'
else:
pass
if path.exists(currentPath + '/places'):
currentPath = currentPath + '/places'
if path.exists(currentPath + '/48'):
currentPath = currentPath + '/48'
elif path.exists(currentPath + '/scalable'):
currentPath = currentPath + '/scalable'
else:
pass
else:
pass
files = [f for f in listdir(currentPath) if isfile(join(currentPath, f))]
for file in files:
if ".svg" in file:
allFiles.append({
"name": file.replace('.svg', ''),
"path": currentPath + '/' + file
})
# allFiles.append(file)
else:
continue
return allFiles
window = MainWindow()
# window.set_resizable(False)
window.connect("destroy", Gtk.main_quit)
window.show_all()
Gtk.main()