sudoui 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/python3
  2. import gi, subprocess, os, sys, locale
  3. gi.require_version("Gtk", "3.0")
  4. from gi.repository import Gio, Gtk, Gdk, GLib, GdkPixbuf
  5. translate = {"tr":{0:"Yetkili Çalıştırma",1:"Tamam",2:"Vazgeç"},
  6. "en":{0:"Root Password",1:"OK",2:"Cancel"}}
  7. l = locale.getdefaultlocale()
  8. l = l[0].split("_")[0]
  9. locales = list(translate.keys())
  10. if l not in locales:
  11. l = "en"
  12. _ = translate[l]
  13. class ROR(Gtk.ApplicationWindow):
  14. def __init__(self, *args, **kwargs):
  15. super().__init__(*args, **kwargs)
  16. self.set_title(_[0])
  17. self.set_default_size(300,70)
  18. box = Gtk.VBox()
  19. self.add(box)
  20. i_box = Gtk.HBox()
  21. box.pack_start(i_box,1,1,5)
  22. img = Gtk.Image()
  23. img.set_from_stock(Gtk.STOCK_EXECUTE,Gtk.IconLookupFlags.FORCE_SIZE)
  24. self.image_button = Gtk.Button()
  25. self.image_button.set_image(img)
  26. self.image_button.connect("clicked",self.image_button_clicked)
  27. i_box.pack_start(self.image_button,0,0,5)
  28. self.command_entry = Gtk.Entry()
  29. i_box.pack_start(self.command_entry,1,1,5)
  30. t_box = Gtk.HBox()
  31. box.pack_start(t_box,1,1,5)
  32. key_img = Gtk.Image()
  33. key_img.set_from_stock(Gtk.STOCK_DIALOG_AUTHENTICATION,Gtk.IconLookupFlags.FORCE_SIZE)
  34. t_box.pack_start(key_img,1,1,5)
  35. self.password_entry = Gtk.Entry()
  36. self.password_entry.set_visibility(False)
  37. t_box.pack_start(self.password_entry,1,1,5)
  38. show_hide_button = Gtk.Button()
  39. show_hide_button.set_label("*")
  40. show_hide_button.connect("clicked",self.show_hide_text)
  41. t_box.pack_start(show_hide_button,0,0,5)
  42. b_box = Gtk.HBox()
  43. box.pack_start(b_box,1,1,5)
  44. ok_button = Gtk.Button()
  45. ok_button.set_label(_[1])
  46. ok_button.connect("clicked",self.run_application)
  47. cancel_button = Gtk.Button()
  48. cancel_button.connect("clicked",self.destroy_window)
  49. cancel_button.set_label(_[2])
  50. b_box.pack_start(ok_button,1,1,5)
  51. b_box.pack_start(cancel_button,1,1,5)
  52. self.password_entry.grab_focus()
  53. self.connect("key-press-event",self.key_press)
  54. def image_button_clicked(self,widget):
  55. appslist = ApplicationsList(self)
  56. appslist.set_position(Gtk.PositionType.RIGHT)
  57. appslist.set_relative_to(widget)
  58. appslist.show_all()
  59. appslist.popup()
  60. def get_icon_in_theme(self, icon_name):
  61. try:
  62. icon = self.default_theme.load_icon(icon_name,
  63. 24,
  64. Gtk.IconLookupFlags.FORCE_SIZE)
  65. return icon
  66. except:
  67. return False
  68. def set_command(self,command):
  69. self.command = command
  70. try:
  71. img = Gtk.Image()
  72. icon = self.default_theme.load_icon(self.command,24,Gtk.IconLookupFlags.FORCE_SIZE)
  73. img.set_from_pixbuf(icon)
  74. self.image_button.set_image(img)
  75. except:
  76. pass
  77. self.command_entry.set_text(command)
  78. def key_press(self, widget, event):
  79. key_name = Gdk.keyval_name(event.keyval)
  80. if key_name == "Return" or key_name=="KP_Enter":
  81. self.run_application(None)
  82. elif key_name == "Escape":
  83. self.destroy()
  84. def show_hide_text(self,widget):
  85. if self.password_entry.get_visibility():
  86. self.password_entry.set_visibility(False)
  87. else:
  88. self.password_entry.set_visibility(True)
  89. def run_application(self,widget):
  90. passwd = self.password_entry.get_text()
  91. try:
  92. text = self.command_entry.get_text()
  93. if text != "":
  94. if text == "pcmanfm":
  95. subprocess.Popen(["sh","-c","echo {}|sudo -SE {}".format(passwd,text)], stdout=subprocess.PIPE)
  96. else:
  97. subprocess.Popen(["sh","-c","echo {}|sudo -SEH DISPLAY= {}".format(passwd,text)], stdout=subprocess.PIPE)
  98. self.destroy()
  99. except:
  100. return False
  101. def destroy_window(self,widget):
  102. self.destroy()
  103. class ApplicationsList(Gtk.Popover):
  104. def __init__(self,parent):
  105. super(ApplicationsList,self).__init__()
  106. self.parent = parent
  107. self.set_size_request(240,480)
  108. self.apps = []
  109. #Tüm Uygulamalar
  110. self.all_apps = Gio.DesktopAppInfo.get_all()
  111. #Theme
  112. self.icon_theme = Gtk.IconTheme.get_default()
  113. #Simge Büyüklüğü
  114. self.icon_size = 24
  115. #Ana yerleşim için yatay bir kutu ve penceereye ekleme
  116. main_box = Gtk.VBox()
  117. self.add(main_box)
  118. #Arama için bir entry
  119. self.search_entry = Gtk.SearchEntry()
  120. self.search_entry.connect("search-changed",self.change_search_entry)
  121. main_box.pack_start(self.search_entry,False,True,5)
  122. #Uygulamalar
  123. self.apps_iw_store = Gtk.ListStore(str,GdkPixbuf.Pixbuf,Gio.DesktopAppInfo)
  124. self.apps_iw = Gtk.IconView(model=self.apps_iw_store)
  125. self.apps_iw.set_item_width(130)
  126. self.apps_iw.set_item_orientation(Gtk.Orientation.HORIZONTAL)
  127. self.apps_iw.connect("item-activated",self.apps_iw_activate)
  128. self.apps_iw.set_text_column(0)
  129. self.apps_iw.set_pixbuf_column(1)
  130. self.apps_store_up()
  131. main_box.pack_start(self.set_scroll_win(self.apps_iw),1,1,5)
  132. self.connect("key-press-event",self.key_press)
  133. def key_press(self, widget, event):
  134. key_name = Gdk.keyval_name(event.keyval)
  135. arrow_keys = ["Up","Down","Left","Right"]
  136. if key_name in arrow_keys:
  137. self.apps_iw.grab_focus()
  138. elif key_name == "Return":
  139. self.apps_iw_activate(self.apps_iw,self.apps_iw.get_selected_items()[0])
  140. elif key_name == "Escape":
  141. self.destroy()
  142. else:
  143. if not self.search_entry.is_focus():
  144. self.search_entry.grab_focus()
  145. def change_search_entry(self,widget):
  146. text = widget.get_text().lower()
  147. self.apps_store_up(text)
  148. def set_scroll_win(self,list_):
  149. scroll = Gtk.ScrolledWindow()
  150. scroll.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
  151. scroll.add(list_)
  152. return scroll
  153. def set_iw_select_item(self,iw,index):
  154. try:
  155. model = iw.get_model()
  156. if model != None:
  157. iter = model[index]
  158. iw.select_path(iter.path)
  159. except:
  160. pass
  161. def apps_store_up(self,search=""):
  162. self.apps_iw_store.clear()
  163. for app in self.all_apps:
  164. name = app.get_name()
  165. desc = app.get_description()
  166. if name == None:
  167. name = ""
  168. if desc == None:
  169. desc = ""
  170. if search == "" or search.lower() in name.lower() or search.lower() in desc.lower():
  171. icon_names = [name,name.lower(),app.get_executable()]
  172. icon = app.get_icon()
  173. if icon != None:
  174. icon_string = icon.to_string()
  175. icon_names.insert(0,icon_string)
  176. for n in icon_names:
  177. icon = self.get_icon_in_theme(n)
  178. if icon:
  179. break
  180. if icon:
  181. self.apps_iw_store.append([name,icon,app])
  182. else:
  183. continue
  184. self.set_iw_select_item(self.apps_iw,0)
  185. def get_icon_in_theme(self, icon_name):
  186. try:
  187. icon = self.icon_theme.load_icon(icon_name,
  188. self.icon_size,
  189. Gtk.IconLookupFlags.FORCE_SIZE)
  190. return icon
  191. except:
  192. return False
  193. def apps_iw_activate(self,widget,path):
  194. model = widget.get_model()
  195. icon = model[path][1]
  196. app = model[path][2]
  197. exec_ = app.get_executable()
  198. img = Gtk.Image()
  199. img.set_from_pixbuf(icon)
  200. self.parent.image_button.set_image(img)
  201. self.parent.command_entry.set_text(exec_)
  202. self.destroy()
  203. class Application(Gtk.Application):
  204. def __init__(self, *args, **kwargs):
  205. super().__init__(*args, application_id="mls.akdeniz.edu.tr.gmenu", flags=Gio.ApplicationFlags.HANDLES_COMMAND_LINE, **kwargs)
  206. self.win = None
  207. self.add_main_option("command", ord("c"), GLib.OptionFlags.NONE, GLib.OptionArg.STRING, "Command", None)
  208. def do_start_up(self):
  209. Gtk.Application.do_startup(self)
  210. def do_activate(self):
  211. if not self.win:
  212. self.win = ROR(application=self)
  213. self.win.connect("destroy", Gtk.main_quit)
  214. self.win.show_all()
  215. def do_command_line(self, command_line):
  216. options = command_line.get_options_dict()
  217. options = options.end().unpack()
  218. if self.win == None:
  219. self.do_activate()
  220. if "command" in options:
  221. self.win.set_command(options["command"])
  222. return False
  223. if __name__ == '__main__':
  224. app = Application()
  225. app.run(sys.argv)