2 # -*- coding: utf-8 -*-
3 """ database handler for fMMS
5 @author: Nick Leppänen Larsson <frals@frals.se>
11 from gnome import gnomevfs
13 import fmms_config as fMMSconf
23 class DatabaseHandler:
26 self.config = fMMSconf.fMMS_config()
27 self.pushdir = self.config.get_pushdir()
28 self.mmsdir = self.config.get_mmsdir()
29 self.outdir = self.config.get_outdir()
30 self.db = self.config.get_db_path()
31 self.conn = sqlite3.connect(self.db)
32 self.conn.row_factory = sqlite3.Row
34 c = self.conn.cursor()
35 c.execute("SELECT * FROM revision")
37 if row['version'] != 1:
38 self.create_database_layout()
40 self.create_database_layout()
43 def create_database_layout(self):
45 c.execute("""CREATE TABLE "revision" ("version" INT);""")
46 c.execute("""INSERT INTO "revision" ("version") VALUES ('1');""")
48 c.execute("""CREATE TABLE "push"(
49 "idpush" INTEGER PRIMARY KEY NOT NULL,
50 "transactionid" TEXT NOT NULL,
51 "content_location" TEXT NULL,
53 "msg_type" TEXT NOT NULL,
56 c.execute("""CREATE TABLE "contacts"(
57 "idcontacts" INTEGER PRIMARY KEY NOT NULL,
58 "number" INTEGER NOT NULL,
59 "abook_uid" INTEGER DEFAULT NULL
61 c.execute("""CREATE TABLE "mms"(
62 "id" INTEGER PRIMARY KEY NOT NULL,
63 "pushid" INTEGER DEFAULT NULL,
64 "transactionid" INTEGER DEFAULT NULL,
65 "msg_time" TIMESTAMP DEFAULT NULL,
66 "read" INTEGER DEFAULT NULL,
67 "direction" INTEGER DEFAULT NULL,
68 "size" INT DEFAULT NULL,
69 "contact" INTEGER DEFAULT NULL,
70 "file" TEXT DEFAULT NULL,
73 REFERENCES "push"("idpush"),
75 FOREIGN KEY("contact")
76 REFERENCES "contacts"("idcontacts")
78 c.execute("""CREATE INDEX "mms.pushid" ON "mms"("pushid");""")
79 c.execute("""CREATE INDEX "mms.contact" ON "mms"("contact");""")
80 c.execute("""CREATE TABLE "mms_headers"(
81 "idmms_headers" INTEGER PRIMARY KEY NOT NULL,
82 "mms_id" INTEGER DEFAULT NULL,
83 "header" TEXT DEFAULT NULL,
84 "value" TEXT DEFAULT NULL,
87 REFERENCES "mms"("id")
89 c.execute("""CREATE INDEX "mms_headers.mms_id" ON "mms_headers"("mms_id");""")
90 c.execute("""CREATE TABLE "attachments"(
91 "idattachments" INTEGER PRIMARY KEY NOT NULL,
92 "mmsidattach" INTEGER DEFAULT NULL,
93 "file" TEXT DEFAULT NULL,
94 "hidden" INTEGER DEFAULT NULL,
95 CONSTRAINT "mmsidattach"
96 FOREIGN KEY("mmsidattach")
97 REFERENCES "mms"("id")
99 c.execute("""CREATE INDEX "attachments.mmsidattach" ON "attachments"("mmsidattach");""")
100 c.execute("""CREATE TABLE "push_headers"(
101 "idpush_headers" INTEGER PRIMARY KEY NOT NULL,
102 "push_id" INTEGER DEFAULT NULL,
103 "header" TEXT DEFAULT NULL,
104 "value" TEXT DEFAULT NULL,
106 FOREIGN KEY("push_id")
107 REFERENCES "push"("idpush")
109 c.execute("""CREATE INDEX "push_headers.push_id" ON "push_headers"("push_id");""")
113 def get_push_list(self, types=None):
114 """ gets all push messages from the db and returns as a list
115 containing a dict for each separate push """
116 c = self.conn.cursor()
118 # TODO: better where clause
119 c.execute("select * from push where msg_type != 'm-notifyresp-ind' order by msg_time DESC")
120 pushlist = c.fetchall()
121 for line in pushlist:
123 result['PUSHID'] = line['idpush']
124 result['Transaction-Id'] = line['transactionid']
125 result['Content-Location'] = line['content_location']
126 result['Time'] = line['msg_time']
127 result['Message-Type'] = line['msg_type']
128 c.execute("select * from push_headers WHERE push_id = ?", (line['idpush'],))
130 result[line2['header']] = line2['value']
132 retlist.append(result)
136 def insert_push_message(self, pushlist):
137 """ Inserts a push message (from a list)
138 Returns the id of the inserted row
141 c = self.conn.cursor()
144 transid = pushlist['Transaction-Id']
145 del pushlist['Transaction-Id']
146 contentloc = pushlist['Content-Location']
147 del pushlist['Content-Location']
148 msgtype = pushlist['Message-Type']
149 del pushlist['Message-Type']
151 print "No transid/contentloc/message-type, bailing out!"
153 fpath = self.pushdir + transid
154 vals = (transid, contentloc, msgtype, fpath)
155 c.execute("insert into push (transactionid, content_location, msg_time, msg_type, file) VALUES (?, ?, datetime('now'), ?, ?)", vals)
158 print "inserted row as:", pushid
160 for line in pushlist:
161 vals = (pushid, line, str(pushlist[line]))
162 c.execute("insert into push_headers (push_id, header, value) VALUES (?, ?, ?)", vals)
167 def insert_push_send(self, pushlist):
168 """ Inserts a push message (from a list)
169 Returns the id of the inserted row
172 c = self.conn.cursor()
175 transid = pushlist['Transaction-Id']
176 del pushlist['Transaction-Id']
177 msgtype = pushlist['Message-Type']
178 del pushlist['Message-Type']
180 print "No transid/message-type, bailing out!"
182 fpath = self.outdir + transid
183 vals = (transid, 0, msgtype, fpath)
184 c.execute("insert into push (transactionid, content_location, msg_time, msg_type, file) VALUES (?, ?, datetime('now'), ?, ?)", vals)
187 print "inserted row as:", pushid
189 for line in pushlist:
190 vals = (pushid, line, str(pushlist[line]))
191 c.execute("insert into push_headers (push_id, header, value) VALUES (?, ?, ?)", vals)
196 def link_push_mms(self, pushid, mmsid):
197 c = self.conn.cursor()
198 c.execute("update mms set pushid = ? where id = ?", (pushid, mmsid))
202 def get_push_message(self, transid):
203 """ retrieves a push message from the db and returns it as a dict """
204 c = self.conn.cursor()
207 c.execute("select * from push WHERE transactionid = ? LIMIT 1;", vals)
210 pushid = line['idpush']
211 retlist['Transaction-Id'] = line['transactionid']
212 retlist['Content-Location'] = line['content_location']
213 retlist['Message-Type'] = line['msg_type']
214 retlist['Time'] = line['msg_time']
215 retlist['File'] = line['file']
216 retlist['PUSHID'] = pushid
219 c.execute("select * from push_headers WHERE push_id = ?;", (pushid, ))
231 def is_mms_downloaded(self, transid):
232 c = self.conn.cursor()
235 c.execute("select * from mms where `transactionid` = ?;", vals)
244 def is_message_read(self, transactionid):
245 c = self.conn.cursor()
246 vals = (transactionid,)
248 c.execute("select read from mms where `transactionid` = ?;", vals)
250 isread = line['read']
256 def insert_mms_message(self, pushid, message, direction=MSG_DIRECTION_IN):
257 """Takes a MMSMessage object as input, and optionally a MSG_DIRECTION_*
258 Returns the newly inserted rows id.
262 mmslist = message.headers
263 attachments = message.attachments
265 c = self.conn.cursor()
268 transid = mmslist['Transaction-Id']
269 del mmslist['Transaction-Id']
270 if direction == MSG_DIRECTION_OUT:
271 basedir = self.outdir + transid
273 basedir = self.mmsdir + transid
275 fpath = basedir + "/message"
276 size = os.path.getsize(fpath)
278 print "No transid/message-type, bailing out!"
281 time = mmslist['Date']
284 time = "datetime('now')"
287 vals = (pushid, transid, time, isread, direction, size, contact, fpath)
288 c.execute("insert into mms (pushid, transactionid, msg_time, read, direction, size, contact, file) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", vals)
291 print "inserted row as:", mmsid
295 vals = (mmsid, line, str(mmslist[line]))
296 c.execute("insert into mms_headers (mms_id, header, value) VALUES (?, ?, ?)", vals)
298 attachpaths = basedir + "/"
300 # insert the attachments
301 for line in attachments:
303 filetype = gnomevfs.get_mime_type(attachpaths + line)
304 (fname, ext) = os.path.splitext(line)
306 # These files should be "hidden" from the user
307 if ext.startswith(".smil") or filetype == "application/smil":
309 vals = (mmsid, line, hidden)
310 c.execute("insert into attachments (mmsidattach, file, hidden) VALUES (?, ?, ?)", vals)
312 #print "inserted", vals
316 def get_mms_attachments(self, transactionid, allFiles=False):
317 c = self.conn.cursor()
318 mmsid = self.get_mmsid_from_transactionid(transactionid)
321 c.execute("select * from attachments where mmsidattach == ?", (mmsid,))
323 c.execute("select * from attachments where mmsidattach == ? and hidden == 0", (mmsid,))
326 filelist.append(line['file'])
331 def get_mms_headers(self, transactionid):
332 c = self.conn.cursor()
333 mmsid = self.get_mmsid_from_transactionid(transactionid)
336 c.execute("select * from mms WHERE id = ? LIMIT 1;", (mmsid,))
339 retlist['Transaction-Id'] = line['transactionid']
340 retlist['Time'] = line['msg_time']
343 c.execute("select * from mms_headers WHERE mms_id = ?;", (mmsid, ))
350 def get_mmsid_from_transactionid(self, transactionid):
351 c = self.conn.cursor()
352 c.execute("select * from mms where transactionid == ?", (transactionid, ))
360 def get_direction_mms(self, transid):
361 c = self.conn.cursor()
362 c.execute("select direction from mms where transactionid = ?", (transid, ))
365 direction = res['direction']
371 def get_pushid_from_transactionid(self, transactionid):
372 c = self.conn.cursor()
373 c.execute("select * from push where transactionid == ?", (transactionid, ))
376 mmsid = res['idpush']
381 def delete_mms_message(self, transactionid):
382 c = self.conn.cursor()
383 mmsid = self.get_mmsid_from_transactionid(transactionid)
385 c.execute("delete from mms where id == ?", (mmsid,))
386 c.execute("delete from attachments where mmsidattach == ?", (mmsid,))
387 c.execute("delete from mms_headers where mms_id == ?", (mmsid,))
390 def delete_push_message(self, transactionid):
391 c = self.conn.cursor()
392 pushid = self.get_pushid_from_transactionid(transactionid)
394 c.execute("delete from push where idpush == ?", (pushid,))
395 c.execute("delete from push_headers where push_id == ?", (pushid,))
399 if __name__ == '__main__':
400 db = DatabaseHandler()
402 #print db.get_push_list()
403 #print db.get_mms_headers("1tid46730354431_2zzhez")