added a menubar
[marvin.git] / marvin / model.py
blob70152da404f208ae13918e8d467027bb2ed23657
1 ##
2 ## model.py
3 ## Login : <freyes@yoda>
4 ## Started on Tue Jul 1 20:50:04 2008 Felipe Reyes
5 ## $Id$
6 ##
7 ## Copyright (C) 2008 Felipe Reyes
8 ##
9 ## This file is part of Marvin.
11 ## Marvin is free software: you can redistribute it and/or modify
12 ## it under the terms of the GNU General Public License as published by
13 ## the Free Software Foundation, either version 3 of the License, or
14 ## (at your option) any later version.
16 ## Marvin is distributed in the hope that it will be useful,
17 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ## GNU General Public License for more details.
21 ## You should have received a copy of the GNU General Public License
22 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
24 import gobject
25 import gtk
26 import gtk.gdk
27 import gnomevfs
28 import gconf
29 import md5
30 import sqlite3
31 import os.path
32 import datetime
33 import time
35 ##logging system
36 import logging
37 log = logging.getLogger('model')
39 #conn = sqlite3.connect('/home/freyes/photos.db')
40 try:
41 import EXIF
42 except:
43 curr_dir = os.path.abspath(".")
44 sys.path.append(curr_dir)
45 import EXIF
48 class Photo(gobject.GObject, object):
49 """Contains the information of a photo
50 """
51 def __init__(self, id, uri, pixbuf_size=64, cursor=None):
52 """
54 Arguments:
55 - `id`: the id of the photo
56 """
57 gobject.GObject.__init__(self)
59 if not os.path.isfile(gnomevfs.get_local_path_from_uri(uri)):
60 print gnomevfs.get_local_path_from_uri(uri)
61 raise RuntimeError("File %s not found" % (uri))
63 self._id = id
64 self.uri = uri
66 f = open(self.path, 'rb')
67 tags = EXIF.process_file(f, stop_tag='DateTimeOriginal')
69 if tags.has_key('EXIF DateTimeOriginal'):
70 (aux_date, aux_time) = str(tags['EXIF DateTimeOriginal']).split(' ')
71 (year, month, day) = aux_date.split(':')
72 (hour, minute, secs) = aux_time.split(':')
73 self.original_date = datetime.datetime(int(year),
74 int(month),
75 int(day),
76 int(hour),
77 int(minute),
78 int(secs))
79 else:
80 self.original_date = time.localtime(os.path.getmtime(self.path))
82 self.tags = list()
84 if (cursor is not None) and (id > -1):
85 c.execute("select tag_id from photo_tags where id = ?", self._id)
86 for row in c:
87 c.execute("select name from tags where id=?", row[0])
88 for tag in c:
89 self.tags.append(tag[0])
91 self.thumbnail_path = thumbnail_path_from_uri(self.uri, "large")
93 try:
94 self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (self.thumbnail_path, pixbuf_size, pixbuf_size)
95 except:
96 thumb_pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self.path, 256, 256)
97 thumb_pixbuf.save(self.thumbnail_path, "png", {"quality":"100"})
98 self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (self.thumbnail_path, pixbuf_size, pixbuf_size)
99 log.info("Thumbnail for %s created" % self.uri)
101 def create_thumbnail(self):
102 cmd = "convert '%s' -resize 256x256 '%s'" % (self.path, thumbnail_path_from_uri(self.uri, "large"))
103 print cmd
104 os.system(cmd)
106 ### properties
108 # path
109 def get_path(self):
110 return gnomevfs.get_local_path_from_uri(self.uri)
112 path = property(get_path, None, None, "the path where the photo is stored")
114 gobject.type_register(Photo)
116 class PhotoListStore (gtk.ListStore):
118 def __init__ (self):
120 super(PhotoListStore, self).__init__(Photo)
122 gobject.type_register(PhotoListStore)
124 def thumbnail_path_from_uri(uri, size='normal'):
125 """Construct the path for the thumbnail of the given uri
127 Arguments:
128 - `uri`: the uri that points to the file that is looking for the thumbnail.
129 - `size`: the size of the thumbnail (normal or large)
132 assert isinstance(uri, basestring), \
133 TypeError("The uri must be a str")
135 assert isinstance(size, basestring) and (size == 'large' or size=='normal'), \
136 TypeError("The size for thumbnail can be normal or large")
138 hash = md5.new()
139 hash.update(uri)
140 path = os.path.join(os.path.expanduser('~'), ".thumbnails", size, str("%s.png" % hash.hexdigest()))
142 return path
144 def connect_to_db():
145 db_path = os.path.join(os.path.expanduser('~'), ".gnome2", "f-spot", "photos.db")
146 conn = sqlite3.connect(db_path)
147 return conn
149 class ConfigurationManager(gobject.GObject):
153 def __init__(self):
156 super(ConfigurationManager, self).__init__()
157 self._client = gconf.client_get_default()
158 self._base = "/apps/marvin"
159 self._client.add_dir (self._base, gconf.CLIENT_PRELOAD_NONE)
162 #### properties
163 # window_default_size
164 def set_window_default_size(self, value):
166 assert isinstance(value, tuple) and len(value)==2, "The value must be a tuple of int, like (200, 200)"
167 assert isinstance(value[0], int) and value[0] > 0, "The pair must int > 0"
168 assert isinstance(value[1], int) and value[1] > 0, "The pair must int > 0"
170 self._client.set_pair(self._base + "/window_size",
171 gconf.VALUE_INT, gconf.VALUE_INT,
172 value[0], value[1])
174 def get_window_default_size(self):
175 return self._client.get_pair(self._base + "/window_size",
176 gconf.VALUE_INT, gconf.VALUE_INT)
178 window_default_size = property(get_window_default_size,
179 set_window_default_size,
180 None,
181 "The default size that will use the main window")
183 @classmethod
184 def set_wallpaper(cls, path):
186 base = "/desktop/gnome/background"
187 client = gconf.client_get_default()
188 client.add_dir(base, gconf.CLIENT_PRELOAD_NONE)
190 client.set_string(base + "/picture_filename", path)
192 gobject.type_register(ConfigurationManager)