profiling switching works on linux and maybe on osx, not yet tested
[stack/code/dboxswitch.git] / profhandler.py
index 3d3060f..19a6b97 100755 (executable)
@@ -36,6 +36,7 @@ import re
 import shutil
 import os
 import errno
+import signal
 
 from apperror import AppError
 from settings import appconf
@@ -116,27 +117,58 @@ class ProfHandler():
         else:
             return False
 
-    def activateProfile(self, pname):
-        if pname in self.getProfilesList():
+    def activateProfile(self, ppath):
+        pl = platform.system()
+        if ppath in self.getProfilesList():
             self.stopDropbox()
             try:
-                with open(os.path.join(self.getProfileFolder(), pname)) as pdir:
-                    os.unlink(self.getDropboxDirectory())
-                    os.symlink(os.path.join(self.getProfileFolder(), pname), self.getDropboxDirectory())
+                print self.getDropboxDirectory()
+                print ppath 
+                if pl in ('Linux','Darwin'):
+                    if os.path.exists(self.getDropboxDirectory()):
+                        os.unlink(self.getDropboxDirectory())
+                    os.symlink(ppath, self.getDropboxDirectory())
+                else:
+                    raise NotImplementedError, "Not implemented yet."
             except IOError as e:
-                raise AppError('Error on activating Profile: '+pname)
+                raise AppError('Error on activating Profile: '+ os.path.basename(ppath))
             self.startDropbox()
         else:
             raise AppError("Trying to acrivate non existant profile")
 
     def getDropboxDirectory(self):
         pl = platform.system()
-        if pl == 'Linux':
+        if pl in ('Linux', 'Darwin'):
             return os.path.join(os.path.expanduser('~'),".dropbox")
         elif pl == 'Windows':
+            assert os.environ.has_key('APPDATA'), Exception('APPDATA env variable not found')
+            return os.path.join(os.environ['APPDATA'],'Dropbox')
+        else:
             raise NotImplementedError, "Not implemented yet."
+
+    def stopDropbox(self):
+        """ Stop dropbox Daemon """
+        pl = platform.system()
+        if pl in ('Linux','Darwin'):
+            pidfile = os.path.expanduser("~/.dropbox/dropbox.pid")                    
+            try:                                                                      
+                with open(pidfile, "r") as f:                                         
+                    pid = int(f.read())                                               
+                    os.kill(pid, signal.SIGTERM)
+            except:                                                                   
+                pass
+
+    def startDropbox(self):
+        """ Sart dropbox Daemon """
+
+        pl = platform.system()
+        if pl == 'Linux':
+            try:
+                os.system("dropbox start -i")
+            except:
+                raise AppError(u"Could not start dropbox.")
         elif pl == 'Darwin':
-            raise NotImplementedError, "Not implemented yet."
+            os.system("/Applications/Dropbox.app/Contents/MacOS/Dropbox &")
 
             
 __CSL = None