File: birthdaydialog.py

package info (click to toggle)
jalali-calendar 1.6.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 372 kB
  • ctags: 148
  • sloc: python: 1,462; xml: 1,315; makefile: 57
file content (238 lines) | stat: -rw-r--r-- 7,994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
##
#    
#    Copyright (C) 2007  Mola Pahnadayan
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
##

import os
import pygtk
pygtk.require('2.0')

import gtk,gtk.glade
import gtk.gdk
import calendar
import os.path
import sys
import locale
from xml.dom.minidom import getDOMImplementation, parse
from xml.parsers.expat import ExpatError
from string import strip, lower

def _get_text(nodelist):
    rc = u""
    name = nodelist.nodeName
    for node in nodelist.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc+ node.data
    return name,rc.strip()

imagelist=["balloons-aj.png","marid.png","shabdar.png"]
extrafile = "/.jcal_extra.xml"
DATA_DIR="/usr/share/jalali-calendar"

def get_home():
        return os.environ.get('HOME', '')
    
class birthdialog:
    
    def __init__(self):
        self.glade=gtk.glade.XML(DATA_DIR+"/interface.glade","dialog1")
        self.bwindow=self.glade.get_widget("dialog1")
        dict={"on_closebutton1_clicked":self.bquit,
               "on_dialog1_destroy":self.bquit,
               "on_button1_clicked":self.element_add,
               "on_button3_clicked":self.element_delete,
               "on_button2_clicked":self.element_edit,
               "on_treeview1_cursor_changed":self.element_change
               }
        self.glade.signal_autoconnect(dict)
        
        self.combobox1=self.glade.get_widget("combobox1")
        self.spinbutton1=self.glade.get_widget("spinbutton1")
        self.combobox2=self.glade.get_widget("combobox2")
        self.entry1=self.glade.get_widget("entry1")
        self.edit_but=self.glade.get_widget("button2")

        self.treestore = gtk.ListStore(gtk.gdk.Pixbuf,str,str,int,int)
        self.treeview=self.glade.get_widget("treeview1")
        self.treeview.set_model(self.treestore)
        
        cell0 = gtk.CellRendererPixbuf()
        col = gtk.TreeViewColumn("",cell0,pixbuf = 0)
        self.treeview.append_column(col)

        cell1 = gtk.CellRendererText()
        col = gtk.TreeViewColumn("تاریخ",cell1,text=1)
        self.treeview.append_column(col)
        self.treeview.set_search_column(1)

        cell3 = gtk.CellRendererText()
        col = gtk.TreeViewColumn("توضیح",cell3,text=2)
        self.treeview.append_column(col)
        
        self.db=None
        self.fxml = None
        self.getfile()
        self.loadlist()
        self.selected_cursor = None
        self.combobox1.set_active(0)
        self.combobox2.set_active(3)
        
    def getfile(self):
        home = get_home()
        if not(os.path.isfile(home+extrafile)):
            self.db=None
            return
        calfile=home+extrafile
        self.fxml = parse(calfile)
        self.db=self.fxml.getElementsByTagName("day")
    
    def loadlist(self):
        if (self.db==None):
            return None
        element_code=1
        pix = gtk.Image()
        for record in self.db:
            list = []
            for element in record.childNodes:
                if element.nodeType != element.TEXT_NODE:
                    if element.nodeType != element.TEXT_NODE:
                        name, data = _get_text(element)
                        if (name=="num"):
                            sp=data.split("/")
                            list.append(sp[0])
                            list.append(sp[1])
                        if (name=="kind"):
                            list.append(data)
                        if (name=="desc"):
                            list.append(data)
            try:
                file = DATA_DIR+"/"+imagelist[int(list[2])]
                pix.set_from_file(file)
                p=pix.get_pixbuf()
            except:
                p=None
            self.treestore.append([p,list[0]+"/"+list[1],list[3],element_code,int(list[2])])
            element_code+=1
        if element_code==1:
            self.edit_but.set_property('sensitive', False)

    def element_add(self,obj,data=None):
        if not(os.path.isfile(get_home()+extrafile)):
            self.create_homefile()
        self.add()
        self.treestore.clear()
        self.getfile()
        self.loadlist()
        
    def element_delete(self,obj,data=None):
        self.delete()
        self.treestore.clear()
        self.getfile()
        self.loadlist()

    def element_edit(self,obj,data=None):
        self.delete()
        self.add()
        self.treestore.clear()
        self.getfile()
        self.loadlist()
        self.entry1.set_text("")
        
    def add(self):
        if self.fxml==None:
            return
        newday = self.fxml.createElement("day")
        field_day = self.fxml.createElement("num")
        mon = str(self.combobox1.get_active()+1)
        day = str(int(self.spinbutton1.get_value()))
        field_day_text = self.fxml.createTextNode(mon+"/"+day)
        
        field_kind = self.fxml.createElement("kind")
        kind = str(self.combobox2.get_active())
        field_kind_text = self.fxml.createTextNode(kind)

        field_desc = self.fxml.createElement("desc")
        text = self.entry1.get_text()
        field_desc_text = self.fxml.createTextNode(text)

        field_day.appendChild(field_day_text)
        field_kind.appendChild(field_kind_text)
        field_desc.appendChild(field_desc_text)
        
        newday.appendChild(field_day)
        newday.appendChild(field_kind)
        newday.appendChild(field_desc)
        
        db2=self.fxml.getElementsByTagName("customday")[0]
        db2.appendChild(newday)
        
        libxml = open(get_home()+extrafile,'w')
        libxml.write(self.fxml.toprettyxml(" "))
        libxml.close()
        
        self.entry1.set_text("")
        
    def element_change(self,obj,data=None):
        selection=obj.get_selection()
        (mode,iter)=selection.get_selected()
        self.selected_cursor=mode.get(iter,3)[0]
        sp= mode.get(iter,1)[0].split("/")
        self.combobox1.set_active(int(sp[0])-1)
        self.spinbutton1.set_value(int(sp[1]))
        self.combobox2.set_active(int(mode.get(iter,4)[0]))
        self.entry1.set_text(mode.get(iter,2)[0])
        self.edit_but.set_property('sensitive', True)
        
        
    def delete (self):
        if (self.db==None):
            return None
        el_code=1
        find =False

        db2=self.fxml.getElementsByTagName("customday")[0]
        for record in db2.getElementsByTagName("day"):
            for element in record.childNodes:
                if (el_code==self.selected_cursor):
                    removeday = db2.removeChild(record)
                    removeday.unlink()
                    find = True
                    break
            if find ==True:
                break
            el_code+=1

        libxml = open(get_home()+extrafile,'w')
        libxml.write(self.fxml.toprettyxml(" "))
        libxml.close()
        
    def create_homefile(self):
        xml = open(get_home()+extrafile,'w')
        xml.write('<?xml version="1.0" ?>')
        xml.write("")
        xml.write('<customday>')
        xml.write("")
        xml.write('</customday>')
        xml.close()
        self.fxml = parse(get_home()+extrafile)

        
    def bquit(self,obg,data=None):
        self.bwindow.destroy()