saving to and loading from db works :-)
[lilass.git] / lilass
diff --git a/lilass b/lilass
index 7b94e188e7aa5e5eecd9b0896854fcee551cbf65..42c15c67775b08774cfb8d3ba3a73f10a1300079 100755 (executable)
--- a/lilass
+++ b/lilass
@@ -16,9 +16,9 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
-import argparse, sys, os, re, subprocess
+import argparse, sys, os, os.path, shutil, re, subprocess
 from enum import Enum
 from enum import Enum
-import gui, screen
+import gui, screen, util, database
 frontend = gui.getFrontend("cli") # the fallback, until we got a proper frontend. This is guaranteed to be available.
 
 
 frontend = gui.getFrontend("cli") # the fallback, until we got a proper frontend. This is guaranteed to be available.
 
 
@@ -30,7 +30,6 @@ def commonInternalConnectorNames():
         for suffix in commonInternalConnectorSuffices:
             yield prefix+suffix
 
         for suffix in commonInternalConnectorSuffices:
             yield prefix+suffix
 
-
 # Load a section-less config file: maps parameter names to space-separated lists of strings (with shell quotation)
 def loadConfigFile(filename):
     import shlex
 # Load a section-less config file: maps parameter names to space-separated lists of strings (with shell quotation)
 def loadConfigFile(filename):
     import shlex
@@ -79,6 +78,18 @@ def situationByConfig(config):
     # run!
     return screen.ScreenSituation(internalConnectors, config.get('externalConnectors'))
 
     # run!
     return screen.ScreenSituation(internalConnectors, config.get('externalConnectors'))
 
+class ShowLevels(Enum):
+    ONEXTERNAL = ("on-external")
+    ONNEW = ("on-new")
+    ONERROR = ("on-error")
+    def __init__(self, text):
+        # auto numbering
+        cls = self.__class__
+        self._value_ = len(cls.__members__) + 1
+        self.text = text
+    @classmethod
+    def getNames(cls):
+        return list(x.text for x in cls)
 
 # if we run top-level
 if __name__ == "__main__":
 
 # if we run top-level
 if __name__ == "__main__":
@@ -100,17 +111,39 @@ if __name__ == "__main__":
         parser.add_argument("-i", "--internal-only",
                             dest="internal_only", action='store_true',
                             help="Enable internal screen, disable all the others.")
         parser.add_argument("-i", "--internal-only",
                             dest="internal_only", action='store_true',
                             help="Enable internal screen, disable all the others.")
+        parser.add_argument("-s", "--show",
+                            dest="show", choices=ShowLevels.getNames(),
+                            help="In which situations should the UI be displayed?")
         cmdArgs = parser.parse_args()
     
         # load frontend early (for error mssages)
         frontend = gui.getFrontend(cmdArgs.frontend)
         
         cmdArgs = parser.parse_args()
     
         # load frontend early (for error mssages)
         frontend = gui.getFrontend(cmdArgs.frontend)
         
+        # find files
+        ## find config file
+        legacyConfigFilePath = os.getenv('HOME') + '/.lilass.conf'
+        configDirectory = util.getConfigDirectory()
+        configFilePath = os.path.join(configDirectory, "lilass.conf")
+        if os.path.isfile(legacyConfigFilePath) and not os.path.isfile(configFilePath):
+            # looks like we just upgraded to a new version of lilass
+            util.mkdirP(configDirectory)
+            shutil.move(legacyConfigFilePath, configFilePath)
+        ## find database
+        dataDirectory = util.getDataDirectory()
+        util.mkdirP(dataDirectory)
+        databaseFilePath = os.path.join(dataDirectory, "collected_data.sqlite")
+
         # load configuration
         # load configuration
-        config = loadConfigFile(os.getenv('HOME') + '/.lilass.conf')
+        config = loadConfigFile(configFilePath)
         
         # see what situation we are in
         situation = situationByConfig(config)
         
         
         # see what situation we are in
         situation = situationByConfig(config)
         
+        # fetch info from the database
+        # if it is too slow to open the DB twice (reading and saving), we can keep it open all the time
+        with database.Database(databaseFilePath) as db:
+            situation.fetchDBInfo(db)
+        
         # construct the ScreenSetup
         setup = None
         if not cmdArgs.internal_only and situation.externalResolutions() is not None:
         # construct the ScreenSetup
         setup = None
         if not cmdArgs.internal_only and situation.externalResolutions() is not None:
@@ -133,9 +166,11 @@ if __name__ == "__main__":
                 # ask the user
                 setup = frontend.setup(situation)
                 if setup is None: sys.exit(1) # the user canceled
                 # ask the user
                 setup = frontend.setup(situation)
                 if setup is None: sys.exit(1) # the user canceled
+                with database.Database(databaseFilePath) as db:
+                    situation.putDBInfo(db, setup)
         else:
             # use first resolution of internal connector
         else:
             # use first resolution of internal connector
-            setup = screen.ScreenSetup(intResolution = situation.internalResolutions()[0], extResolution = None)
+            setup = screen.ScreenSetup(intResolution = situation.internalConnector.getResolutionList()[0], extResolution = None)
         
         # call xrandr
         xrandrCall = situation.forXrandr(setup)
         
         # call xrandr
         xrandrCall = situation.forXrandr(setup)