Change the max grid size before changing the size (fixes bug #12292). Don't rotate...
[drlaunch] / src / widget.py
index 53bf2df..35141bb 100755 (executable)
@@ -42,8 +42,11 @@ from hildondesktop import *
 from gtk import gdk
 from math import pi
 import cairo
+import gconf
 import time
 
+from subprocess import Popen,PIPE
+
 from portrait import FremantleRotation
 import launcher
 from xdg.IconTheme import getIconPath
@@ -53,79 +56,238 @@ import config
 import apps
 from icon import Icon
 from icongrid import IconGrid
+from sig import Disconnector
 
 # Restore path
 sys.path=orig_path
 
 # IconGrid must be before HomePluginItem for its connect()
 # and do_button_*() to override those of HomePluginItem
-class DrlaunchPlugin(IconGrid, HomePluginItem, FremantleRotation):
+class DrlaunchPlugin(IconGrid, HomePluginItem, FremantleRotation, Disconnector):
     def __init__(self):
        IconGrid.__init__(self)
        HomePluginItem.__init__(self)
-       FremantleRotation.__init__(self, 'DrlaunchPlugin')
+       FremantleRotation.__init__(self, 'DrlaunchPlugin',
+           mode=FremantleRotation.AUTOMATIC, dontrotate=True)
+       Disconnector.__init__(self)
+
+       self.winConfig=None
+
+       self.gconf=gconf.client_get_default()
+
+       self.set_settings(True)
+
+       self.id=None
+       self.config=None
+
+       self.reset_mode()
+
+    def get_id0(self):
+       """If this is called from the constructor then the program
+       core dumps """
+       aid=self.get_applet_id()
+
+       # Get desktop activity if D.A.M. is present
+       
+       act="/usr/bin/activity"
+
+       if os.path.exists(act):
+           r=Popen([act, "current"], stdout=PIPE).communicate()
+           activity=r[0].strip()
+       else:
+           activity=""
+
+       ret="%s-%s" % (aid, activity)
+
+       return(ret)
+
+    def get_id(self):
+       if self.id==None:
+           self.id=self.get_id0()
+
+       return(self.id)
 
+    def get_config(self):
+       if self.config==None:
+           id=self.get_id()
+           self.config=config.Config(id)
+
+       return(self.config)
+
+    def get_desktop_orientation(self):
+       """
+       Return desktop orientation
+
+       NOTE: This is the desktop orientation as it was introduced in CSSU.
+       Not the device orientation.
+
+       @return "portrait" or "landscape"
+       """
+
+       sw=gdk.screen_width()
+       sh=gdk.screen_height()
+
+       if sw>=sh:
+           ret='landscape'
+       else:
+           ret='portrait'
+
+       return(ret)
+
+    def is_rotating_desktop(self):
+       """
+       Check whether the desktop will change to portrait mode, as
+       added in CSSU.
+
+       @return True/False
+       """
+
+       c=self.gconf
+
+       # This returns False if the key doesn't exist
+       ret=c.get_bool('/apps/osso/hildon-desktop/ui_can_rotate')
+
+       return(ret)
+
+    def do_realize(self):
        launcher.init()
+       config=self.get_config()
        config.load()
+
+       IconGrid.do_realize(self, config)
+
        self.setSize(config.getSize())
        self.reloadIcons()
 
-       self.set_settings(True)
-       self.connect('show-settings', self.slot_show_settings)
-       self.connect('long-press', self.signalLongpress)
-
-    def do_realize(self):
        screen=self.get_screen()
        self.set_colormap(screen.get_rgba_colormap())
        self.set_app_paintable(True)
 
+       self.c(self, 'show-settings', self.slot_show_settings)
+       self.c(self, 'long-press', self.signalLongpress)
+       self.c(self, 'click', self.signalClick)
+       self.c(self, 'notify', self.signalNotify)
+
        HomePluginItem.do_realize(self)
 
     def on_orientation_changed(self, orientation):
-       print "orch:", orientation
+       # Avoid bugs
+       if orientation==None or len(orientation)==0:
+           return
+
+       # Get the first character of the string (l/p)
        o=orientation[0]
+
+       # Get desktop orientation
+       #do=self.get_desktop_orientation()
+
+       # Is desktop rotation (per CSSU) enabled?
+       rd=self.is_rotating_desktop()
+
+       #print "desktop: %s / %s, device: %s" % (do, rd, o)
+
+       # In case of a rotating desktop, force orientation to be
+       # 'landscape'
+       if rd:
+           o='l'
+
        self.setMode(o)
 #      self.queue_draw()
 
     def do_expose_event(self, event):
        IconGrid.do_expose_event(self, event)
        HomePluginItem.do_expose_event(self, event)
-
-    def do_buttonn_press_event(self, event):
-       print "press0"
+       self.reset_mode()
 
     def slot_show_settings(self, dt):
-       print "settings", dt
-       s=WinConfig()
+       if self.winConfig!=None:
+           # Doesn't work
+           # self.winConfig.show_all()
+           return
+
+       s=WinConfig(self.get_config())
        s.show_all()
-       s.connect('destroy', self.slotConfigDestroy)
+       #s.c(s, 'delete-event', self.slotConfigDestroy)
+       self.c(s, 'delete-event', self.slotConfigDestroy)
+       #s.connect('destroy', self.slotConfigDestroy)
+       self.winConfig=s
 
-    def slotConfigDestroy(self, sender):
-       print "destroy", sender
+    def slotConfigDestroy(self, sender, event):
+#      print "Sender:", sender
        dt=sender.getData()
-       print "dt:", dt
-       config.setSize(dt['size'])
-       config.setApps(dt['apps'])
-       config.save()
+
+       # Disconnect signals for that object in order to be deleted
+       self.dis_finish(self.winConfig)
+       #self.winConfig.finish()
+       #self.winConfig.destroy()
+
+       self.winConfig=None
+
+       cfg=self.get_config()
+
+       cfg.setSize(dt['size'])
+       cfg.setApps(dt['apps'])
+       cfg.setIndiv(dt['indiv'])
+       cfg.setLongpress(dt['longpress'])
+       cfg.setAnimate(dt['animate'])
+       cfg.setNoBg(dt['nobg'])
+       cfg.setThemeBg(dt['themebg'])
+       cfg.setIconSize(dt['iconsize'])
+       cfg.setIconPadding(dt['iconpadding'])
+       cfg.setIconMargin(dt['iconmargin'])
+       cfg.save()
        
        # Resize widget
+       self.icons.resizeMax()
        self.setSize(dt['size'])
        self.reloadIcons()
 
-#      self.queue_draw()
+       # Free memory that is used for animations if animations are disabled
+       if not dt['animate']:
+           self.clearAnimationCache()
+
+       # Free memory of backgrounds in case they changed
+       self.clearBgCache()
+
+       self.queue_draw()
+
+#      print "slot-config-destroy-end"
+
+       return(False)
+
+    def handle_click(self, sender, icon):
+       """ common handler for longpress and click """
+       if icon.appname!=None and icon.appname!='':
+           launcher.launch(icon.appname)
 
     def signalLongpress(self, sender, icon):
-       print "launch:", icon.name
-       launcher.launch(icon.name)
+       self.handle_click(sender, icon)
+
+    def signalClick(self, sender, icon):
+       config=self.get_config()
+
+       if not config.getLongpress():
+           self.handle_click(sender, icon)
+
+    def signalNotify(self, sender, property):
+       if property.name=='is-on-current-desktop':
+           v=self.get_property(property.name)
+           if v and self.draw_pending:
+               self.queue_draw()
+
+    def resize2(self):
+       config=self.get_config()
 
-    def resize(self):
-       w=(self.size * config.iconsize) + \
-           (self.size * config.iconspace)
-       self.set_size_request(w, w)
+       w=(self.size[0] * config.iconsize) + \
+           (self.size[0] * config.getIconSpace())
+       h=(self.size[1] * config.iconsize) + \
+           (self.size[1] * config.getIconSpace())
+       self.set_size_request(w, h)
+       self.resize(w, h)
 
     def setSize(self, size):
        IconGrid.setSize(self, size)
-       self.resize()
+       self.resize2()
 
 hd_plugin_type = DrlaunchPlugin