Advertisement
Guest User

paxman

a guest
Nov 24th, 2008
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.24 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import os
  4. import pygtk
  5. pygtk.require('2.0')
  6. import gtk
  7. import time
  8. import string
  9.  
  10. class StatusIcc:
  11.    
  12. #menu handlers
  13.    
  14.     #quit program
  15.     def quit_cb(self,widget, data = None):
  16.              if data:
  17.                  data.set_visible(False)
  18.              gtk.main_quit()
  19.          
  20.     #show menu    
  21.     def popup_menu_cb(self, widget, button, time, data = None):
  22.              if button == 3:
  23.                  if data:
  24.                      data.show_all()
  25.                      data.popup(None, None, None, 3, time)
  26.              pass
  27.    
  28.     #about
  29.     def about(self,widget):
  30.         dia = gtk.AboutDialog()
  31.         dia.set_name("Scroter")
  32.         dia.set_program_name("Scroter")
  33.         dia.set_authors('paxman\n')
  34.         dia.set_version("0.5")
  35.         dia.run()
  36.         dia.destroy()
  37.        
  38.     #settings
  39.     def settings(self,widget):
  40.         # create a new window
  41.         window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  42.         window.set_size_request(380, 100)
  43.         window.set_title("Scroter settings")
  44.         #window.connect("delete_event", lambda w,e: gtk.main_quit())
  45.  
  46.         #get settings
  47.         settings = self.read_settings()
  48.        
  49.         vbox = gtk.VBox(False, 0)
  50.         window.add(vbox)
  51.         vbox.show()
  52.  
  53.         hbox = gtk.HBox(False, 0)
  54.         vbox.add(hbox)
  55.         hbox.show()
  56.        
  57.         label = gtk.Label("Quality of image, in %:")
  58.         hbox.pack_start(label, True, True, 0)
  59.         label.show()
  60.        
  61.         quality = gtk.Entry()
  62.         quality.set_max_length(50)
  63.         quality.set_text(settings[0])
  64.         hbox.pack_start(quality, True, True, 0)
  65.         quality.show()
  66.        
  67.         hbox = gtk.HBox(False, 0)
  68.         vbox.add(hbox)
  69.         hbox.show()
  70.        
  71.         label = gtk.Label("Console image viewer command:")
  72.         hbox.pack_start(label, True, True, 0)
  73.         label.show()
  74.        
  75.         viewer = gtk.Entry()
  76.         viewer.set_max_length(50)
  77.         viewer.set_text(settings[1])
  78.         hbox.pack_start(viewer, True, True, 0)
  79.         viewer.show()
  80.                    
  81.         button = gtk.Button(stock=gtk.STOCK_SAVE)
  82.         button.connect("clicked",self.write_settings,window,quality,viewer)
  83.         vbox.pack_start(button, True, True, 0)
  84.         button.set_flags(gtk.CAN_DEFAULT)
  85.         button.grab_default()
  86.         button.show()
  87.        
  88.         window.show()
  89.  
  90. #handlers
  91.  
  92.     #get whole screen
  93.     def get_all(self, widget):
  94.  
  95.         # grab the screenshot
  96.         self.grab_it("")            
  97.              
  98.     #get selected window
  99.     def get_window(self, widget):
  100.  
  101.         # grab the screenshot
  102.         #TODO: w/o, w/ border
  103.         self.grab_it("-s")                
  104.  
  105.     #get selected area
  106.     def get_area(self, widget):
  107.    
  108.         # grab the screenshot
  109.         self.grab_it("-s")            
  110.    
  111. #helpet functions
  112.  
  113.     def get_temp_file_name(self):
  114.         # create a unique image file name based on the application PID
  115.         #return random image name?
  116.         imageName = "/tmp/scrotshot.png"
  117.                        
  118.         # return the result
  119.         return imageName            
  120.  
  121.  
  122.     def grab_it(self, commandParameters):
  123.  
  124.         #delay? do users really need that?
  125.         #delay = 0
  126.        
  127.         #check if directory for images exists
  128.         if not os.path.exists(os.path.expanduser("~/scroter")):
  129.             os.system("mkdir -p ~/scroter")
  130.        
  131.         #name of image - current time
  132.         imagename = time.strftime("%Y-%m-%d_%H-%M-%S")
  133.        
  134.         # grab the screenshot and move it to user home directory
  135.         os.system("scrot ~/scroter/"+ imagename+".png" + " " + commandParameters)
  136.        
  137.         # change image's permissions
  138.         os.system("chmod 0600 ~/scroter/"+ imagename+".png")
  139.        
  140.         #open image with image viewer
  141.         #TODO: settings -> change viewer
  142.         os.system("gpicview ~/scroter/"+ imagename+".png")
  143.    
  144.     def checkSettingsFile(self):
  145.         if not os.path.exists(os.path.expanduser("~/.scroter")):
  146.             os.system("touch ~/.scroter")
  147.             file = open(os.path.expanduser("~/.scroter"),"w")
  148.             file.write("75\n")
  149.             file.write("gpicview")
  150.             file.flush()
  151.             file.close()
  152.            
  153.     def write_settings(self,widget,window,quality,viewer):
  154.         file = open(os.path.expanduser("~/.scroter"),"w")
  155.         print quality.get_text() + " " + viewer.get_text()
  156.         file.write(quality.get_text()+"\n")
  157.         file.write(viewer.get_text())
  158.         file.flush()
  159.         file.close()
  160.         window.hide()
  161.        
  162.     def read_settings(self):
  163.         settings = []
  164.         file = open(os.path.expanduser("~/.scroter"),"r")
  165.         settings.append(string.rstrip(file.readline()))
  166.         settings.append(string.rstrip(file.readline()))
  167.         file.close()
  168.         return settings
  169.            
  170.     def __init__ (self):
  171.        
  172.         statusIcon = gtk.StatusIcon()
  173.        
  174.         #check if settings file exists; if not, make one
  175.         self.checkSettingsFile()
  176.        
  177.         menu = gtk.Menu()            
  178.         menu_items = (
  179.                         ("Area", self.get_area),
  180.                         ("Window", self.get_window),
  181.                         ("All", self.get_all),
  182.                         None,
  183.                         ("Settings", self.settings),
  184.                         ("About", self.about),
  185.                         ("Exit", self.quit_cb),
  186.                         );
  187.                    
  188.         for i in menu_items:
  189.             if i is None:
  190.                 menu.append(gtk.SeparatorMenuItem());
  191.             else:
  192.                 menu_items = gtk.MenuItem( i[0] );
  193.                 menu_items.connect('activate', i[1] );
  194.                 menu.append( menu_items );
  195.  
  196.         #image by Iconspedia (http://www.iconspedia.com/icon/digital-camera-610.html , http://mugenb16.deviantart.com/art/Massive-Media-Icons-Win-48654751)
  197.         statusIcon.set_from_file("camera.png")
  198.         statusIcon.set_tooltip("Scroter - scrot frontend")
  199.         statusIcon.connect('popup-menu', self.popup_menu_cb,menu)
  200.         statusIcon.set_visible(True)
  201.        
  202.         gtk.main()
  203.        
  204. if __name__ == "__main__":
  205.    statusicon = StatusIcc()
  206.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement