OSDN Git Service

Save window size
authorHirotaka Kawata <hirotaka@notebook.techno-st.net>
Fri, 26 Feb 2010 04:48:30 +0000 (13:48 +0900)
committerHirotaka Kawata <hirotaka@notebook.techno-st.net>
Fri, 26 Feb 2010 04:48:30 +0000 (13:48 +0900)
main.py
saveconfig.py [new file with mode: 0644]

diff --git a/main.py b/main.py
index 4319f69..9421dfa 100644 (file)
--- a/main.py
+++ b/main.py
@@ -5,6 +5,7 @@ import pygtk
 pygtk.require('2.0')
 import gtk
 
+import sys
 import threading
 import random
 import time
@@ -13,6 +14,7 @@ from objects import GtkObjects
 from timeline import timeline
 from twitterapi import twitterapi
 from iconstore import IconStore
+from saveconfig import save_configs, save_config, get_config
 
 # Main Class
 class Main:
@@ -42,11 +44,21 @@ class Main:
         init = threading.Thread(target=self.initialize, args=(keys, maxn))
         init.start()
     
-    def main(self):        
+    def main(self):
         # Gtk Multithread Setup
         gtk.gdk.threads_init()
         gtk.gdk.threads_enter()
+        
+        # settings allocation
+        try:
+            alloc = get_config("DEFAULT", "allocation")
+            alloc = eval(alloc)
+            self.obj.window1.resize(alloc.width, alloc.height)
+        except:
+            print >>sys.stderr, "[Warning] Allocation not defined"        
+        
         self.obj.window1.show_all()
+        
         # Start gtk main loop
         gtk.main()
         gtk.gdk.threads_leave()
@@ -65,6 +77,11 @@ class Main:
     
     # Window close event
     def close(self, widget):
+        # Save Allocation (window position, size)
+        print widget.get_position()
+        alloc = repr(widget.allocation)
+        save_config("DEFAULT", "allocation", alloc)
+        
         gtk.main_quit()
     
     # Get text
diff --git a/saveconfig.py b/saveconfig.py
new file mode 100644 (file)
index 0000000..c060357
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+#-*- coding: utf-8 -*-
+
+import os
+from ConfigParser import SafeConfigParser
+
+def save_config(section, key, value):
+    confp = _open()    
+    confp.set(section, key, value)
+    _close(confp)
+
+def save_configs(conftuple):
+    confp = _open()    
+    for section, key, value in conftuple:
+        confp.set(section, key, str(value))
+    _close(confp)
+
+def get_config(section, keys):
+    confp = _open()
+    items = dict(confp.items(section))
+    return items[keys]
+
+def _open():
+    confp = SafeConfigParser()
+    conf_path = os.path.join(
+        os.path.dirname(__file__), "gwit.conf")
+    
+    if os.path.isfile(conf_path):
+        confp.read(conf_path)
+    
+    return confp
+
+def _close(confp):
+    conf_path = os.path.join(
+        os.path.dirname(__file__), "gwit.conf")
+    
+    fp = open(conf_path, "w")
+    confp.write(fp)
+    fp.close()