deleted blank line
[marvin.git] / marvin / model.py
blob55dfab15dd7af6416b28d1a0762773cacb6e016a
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 md5
29 import sqlite3
30 import os.path
31 import datetime
32 import time
34 ##logging system
35 import logging
36 log = logging.getLogger('model')
38 #conn = sqlite3.connect('/home/freyes/photos.db')
39 try:
40 import EXIF
41 except:
42 curr_dir = os.path.abspath(".")
43 sys.path.append(curr_dir)
44 import EXIF
47 class Photo(gobject.GObject, object):
48 """Contains the information of a photo
49 """
50 def __init__(self, id, uri, pixbuf_size=64, cursor=None):
51 """
53 Arguments:
54 - `id`: the id of the photo
55 """
56 gobject.GObject.__init__(self)
58 if not os.path.isfile(gnomevfs.get_local_path_from_uri(uri)):
59 print gnomevfs.get_local_path_from_uri(uri)
60 raise RuntimeError("File %s not found" % (uri))
62 self._id = id
63 self.uri = uri
65 f = open(self.path, 'rb')
66 tags = EXIF.process_file(f, stop_tag='DateTimeOriginal')
68 if tags.has_key('EXIF DateTimeOriginal'):
69 (aux_date, aux_time) = str(tags['EXIF DateTimeOriginal']).split(' ')
70 (year, month, day) = aux_date.split(':')
71 (hour, minute, secs) = aux_time.split(':')
72 self.original_date = datetime.datetime(int(year),
73 int(month),
74 int(day),
75 int(hour),
76 int(minute),
77 int(secs))
78 else:
79 self.original_date = time.localtime(os.path.getmtime(self.path))
81 self.tags = list()
83 if (cursor is not None) and (id > -1):
84 c.execute("select tag_id from photo_tags where id = ?", self._id)
85 for row in c:
86 c.execute("select name from tags where id=?", row[0])
87 for tag in c:
88 self.tags.append(tag[0])
90 self.thumbnail_path = thumbnail_path_from_uri(self.uri, "large")
92 try:
93 self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (self.thumbnail_path, pixbuf_size, pixbuf_size)
94 except:
95 thumb_pixbuf = gtk.gdk.pixbuf_new_from_file_at_size(self.path, 256, 256)
96 thumb_pixbuf.save(self.thumbnail_path, "png", {"quality":"100"})
97 self.pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (self.thumbnail_path, pixbuf_size, pixbuf_size)
98 log.info("Thumbnail for %s created" % self.uri)
100 def create_thumbnail(self):
101 cmd = "convert '%s' -resize 256x256 '%s'" % (self.path, thumbnail_path_from_uri(self.uri, "large"))
102 print cmd
103 os.system(cmd)
105 ### properties
107 # path
108 def get_path(self):
109 return gnomevfs.get_local_path_from_uri(self.uri)
111 path = property(get_path, None, None, "the path where the photo is stored")
113 gobject.type_register(Photo)
115 class PhotoListStore (gtk.ListStore):
117 def __init__ (self):
119 super(PhotoListStore, self).__init__(Photo)
121 gobject.type_register(PhotoListStore)
123 def thumbnail_path_from_uri(uri, size='normal'):
124 """Construct the path for the thumbnail of the given uri
126 Arguments:
127 - `uri`: the uri that points to the file that is looking for the thumbnail.
128 - `size`: the size of the thumbnail (normal or large)
131 assert isinstance(uri, basestring), \
132 TypeError("The uri must be a str")
134 assert isinstance(size, basestring) and (size == 'large' or size=='normal'), \
135 TypeError("The size for thumbnail can be normal or large")
137 hash = md5.new()
138 hash.update(uri)
139 path = os.path.join(os.path.expanduser('~'), ".thumbnails", size, str("%s.png" % hash.hexdigest()))
141 return path
143 def connect_to_db():
144 db_path = os.path.join(os.path.expanduser('~'), ".gnome2", "f-spot", "photos.db")
145 conn = sqlite3.connect(db_path)
146 return conn