Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Contactos.py
blob: c8528fa63a44268e534ab42f996d27caa2c7147a (plain)
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#! /usr/bin/env python

# Copyright (c) 2011, Walter Vargas <pynash@gmail.com>
# Copyright (c) 2011, Joel Davila <joe74@fedoraproject.org>

#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 3 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, see <http://www.gnu.org/licenses/>.

from gettext import gettext as _
import gtk
import pygtk
import os.path
import sqlite3

import sql_class as conn_db

class Contactos:
    '''Clase Principal'''

    def __init__(self):
        '''Ventana Principal'''
        
        ################### Ventana e Interface ####################
		
	self.window = gtk.Window()
	#self.window.connect("delete-event", self._delete_event)
	self.window.set_title(_('CONTACTOS'))
	self.window.set_size_request(800, 600)
	################### Caja de Texto ####################
        
        self.display_now = []
	self.pulse = 0
	self.fname = ""
	self.lname = ""
	self.id = ""
	self.lista_name = []

	################## Ventana con scroll ##################
        
	self.scrolled_window = gtk.ScrolledWindow()
	
	self.scrolled_window.set_border_width(10)
	self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_ALWAYS)

	################## Contenedor Principal ##################

	self.container = gtk.HBox()

	self.h2 = gtk.HBox()
		
        ###### Vertical 1 ######

	self.vb1 = gtk.VBox()

	###### Aca van la raiz y los datos formales.
	###### creamos el treestore con la columna tipo string como modelo
		
        self.treestore = gtk.TreeStore(str)

	db = conn_db.Conexion()

	self.lista = []

	self.categorias = ['familiares', 'amigos', 'companeros', 'otros']

	for cat in self.categorias:		
		citer = self.treestore.append(None, ['%s' % cat])
			
		lista = db._display_on_tree(cat)

		for i in lista:
			self.lista.append(i)
			for i in range(len(lista)):
				self.treestore.append(citer, ['%s' % lista[i][0] + " " + lista[i][1]])

			#for i in lista:
			#	self.lista.append(i)

			#for i in range(len(lista)):
			#	self.treestore.append(citer, ['%s' % lista[i]])

	###### Creamos el treeview, contenedor de treeviewcolumn y cellrender
	###### encargado de mostrar la vista del treestore
		
        self.treeview = gtk.TreeView(self.treestore)

	self.treeview.connect("row-activated", self._clicar)

		###### Creamos el treeviewcolumn que mostrara el dato
		
        self.tv_column = gtk.TreeViewColumn('Categoria')

		###### Agregamos al treeview la columna
			
        self.treeview.append_column(self.tv_column)

		###### Vamos a crear un objeto para renderizar los datos.
		
        self.cell = gtk.CellRendererText()

		###### Add the cell al tv_column

	self.tv_column.pack_start(self.cell, True)

	self.tv_column.add_attribute(self.cell, 'text', 0)

	self.treeview.set_search_column(0)

	self.tv_column.set_sort_column_id(0)
	self.treeview.set_reorderable(True)

	self.scrolled_window.add_with_viewport(self.treeview)

	self.vb1.pack_start(self.scrolled_window, True, True, 0)
	self.h2.pack_start(self.vb1, True, True, 0)
	self.scrolled_window.show()
	self.treeview.show()

	self.container.pack_start(self.h2, True, True, 0)

		#--------------CENTER OPTIONS CONTAINER-------------#
	self.opcionbox = gtk.VBox()

	self.opcionlabel = gtk.Label("         Opciones        ")

	self.add = gtk.Button("Nuevo Contacto")
	self.add.connect("clicked", self._add_user)

	self.update = gtk.Button("Editar Contacto")
	self.update.connect("clicked", self._update_user)

	self.delete = gtk.Button("Eliminar Contacto")
	self.delete.connect("clicked", self._del_user)

	self.opcionbox.pack_start(self.opcionlabel, False, False, 20)
	self.opcionbox.pack_start(self.add, False, False, 40)
	self.opcionbox.pack_start(self.update, False, False, 40)
	self.opcionbox.pack_start(self.delete, False, False, 40)

	self.container.pack_start(self.opcionbox, False, False, 0)

		#---------------RIGHT CONTAINER-------------#
	self.c_right = gtk.VBox()

		
	self.contacto = gtk.Label("	CONTACTO	")
	self.c_right.pack_start(self.contacto, False, False, 20)


	self.hr1 = gtk.HBox()

	self.vr_2 = gtk.VBox()
	self.containerl1 = gtk.HBox()
	self.lf_name = gtk.Label("Nombres")
	self.containerl1.pack_start(self.lf_name, False, False, 10)

	self.ef_name = gtk.Entry()
	self.ef_name.set_size_request(300, 30)		
	self.containerl1.pack_start(self.ef_name, False, False, 10)

	self.vr_2.pack_start(self.containerl1, False, False, 30)

	self.containerl2 = gtk.HBox()

	self.ll_name = gtk.Label("Apellidos ")
	self.containerl2.pack_start(self.ll_name, False, False, 10)

	self.el_name = gtk.Entry()
	self.el_name.set_size_request(300, 30)		
	self.containerl2.pack_start(self.el_name, False, False, 10)

	self.vr_2.pack_start(self.containerl2, False, False, 30)

	self.hr1.pack_start(self.vr_2, False, False, 20)

	self.pic = gtk.Image()

	#self.pic.set_from_file("./scrimmage1.jpg")

	self.hr1.pack_start(self.pic, False, False, 0)
		
	self.c_right.pack_start(self.hr1, False, False, 10)


	self.hr2 = gtk.HBox()
	self.l_address = gtk.Label(" Direccion")
	self.e_address = gtk.Entry()
	self.e_address.set_size_request(400, 30)		
	self.hr2.pack_start(self.l_address, False, False, 20)
	self.hr2.pack_start(self.e_address, False, False, 30)
	self.c_right.pack_start(self.hr2, False, False, 30)

	self.hr3 = gtk.HBox()
	self.l_phone = gtk.Label(" Telefono   ")
	self.e_phone = gtk.Entry()
	self.e_phone.set_size_request(80, 30)		
	self.hr3.pack_start(self.l_phone, False, False, 20)
	self.hr3.pack_start(self.e_phone, False, False, 20)
	self.c_right.pack_start(self.hr3, False, False, 30)


	self.hr2 = gtk.HBox()
	self.l_ocupation = gtk.Label(" Ocupacion")
	self.e_ocupation = gtk.Entry()
	self.e_ocupation.set_size_request(200, 30)		
	self.hr2.pack_start(self.l_ocupation, False, False, 20)
	self.hr2.pack_start(self.e_ocupation, False, False, 20)
	self.c_right.pack_start(self.hr2, False, False, 30)

	self.hr2 = gtk.HBox()
	self.l_email = gtk.Label("Email      ")
	self.e_email = gtk.Entry()
	self.e_email.set_size_request(200, 30)		
	self.hr2.pack_start(self.l_email, False, False, 25)
	self.hr2.pack_start(self.e_email, False, False, 25)
	self.l_email = gtk.Label("por Ej: midireccion@gmail.com")		
	self.hr2.pack_start(self.l_email, False, False, 20)

	
	self.c_right.pack_start(self.hr2, False, False, 20)

	self.hr2 = gtk.HBox()
	self.l_category = gtk.Label("Categoria  ")
	self.e_category = gtk.Entry()
	self.e_category.set_size_request(200, 30)
	self.hr2.pack_start(self.l_category, False, False, 20)
	self.hr2.pack_start(self.e_category, False, False, 20)
	self.c_right.pack_start(self.hr2, False, False, 20)

		##-----------confirmation--------------------#

	self.confirmation = gtk.HBox()

	self.label_dialog = gtk.Label()
		#self.label_dialog.set_text('HLA')

	self.aceptar = gtk.Button("Guardar Cambios")
	self.aceptar.connect("clicked", self._aceptar)
	self.aceptar.set_sensitive(False)
	self.aceptar.hide()

	self.cancelar = gtk.Button("Cancelar")
	self.cancelar.connect("clicked", self._cancelar)
	self.cancelar.set_sensitive(False)
	self.cancelar.hide()

	self.confirmation.pack_start(self.label_dialog, False, False, 0)
	self.confirmation.pack_start(self.aceptar, False, False, 0)
	self.confirmation.pack_start(self.cancelar, False, False, 0)
	self.c_right.pack_start(self.confirmation, False, False, 0)

		
	self.container.pack_start(self.c_right, False, False, 0)
		
		#-----cargar contacto default-----------#
		
	self._load_contact(1)

		#----------------------- displaying all--------------------------------#

	self.window.add(self.container)
	self.window.show_all()
	
		#--------------finaliza __INIT__----------------------------------------#


	#---- metodo que presentara el elemento clickeado en la lista de arbol ----#
	def _clicar(self, tvcolumn, path, iter):
		opcion = self.treestore[path][0]
		if opcion in self.lista:
			# No tendria porque ir self.lista_name = opcion.split(" ")
			print opcion[0]
			self._load_contact(metodo=0)
		#pass


	def _load_contact(self, metodo=1):
		"""Conect to sql bd file and return a lista"""
		lista=[]
		if metodo:
			db = conn_db.Conexion(1)
			lista = db._show_default(1)
		else:
			#pass
			db = conn_db.Conexion(self.lista_name[0], self.lista_name[1])
			lista = db._show_default(0)
		self.display_now = lista
		self._load_on_entry(self.display_now)

	def _load_on_entry(self, lista):
		"""load on entry values to display"""
		self._editable_entry(False)
		self.ef_name.set_text(lista[0])
		self.el_name.set_text(lista[1])
		self.e_address.set_text(lista[2])
		self.e_phone.set_text(lista[3])
		self.e_ocupation.set_text(lista[4])
		self.e_email.set_text(lista[5])
		self.e_category.set_text(lista[6])
		self.pic.set_from_file('./images/show.jpg')

	def _sensitive_options(self, i):
		self.add.set_sensitive(i)
		self.update.set_sensitive(i)
		self.delete.set_sensitive(i)
		self.opcionlabel.set_sensitive(i)
		self.scrolled_window.set_sensitive(i)

	def _sensitive_confirmation(self, i):
		self.aceptar.set_sensitive(i)
		self.cancelar.set_sensitive(i)

	def _read_datas(self):
		f_name = self.ef_name.get_text().lower()
		l_name = self.el_name.get_text().lower()
		e_address = self.e_address.get_text().lower()
		e_phone = self.e_phone.get_text().lower()
		e_ocupation = self.e_ocupation.get_text().lower()
		e_email = self.e_email.get_text().lower()
		e_category = self.e_category.get_text().lower()
		if self.pulse==1:
			db = conn_db.Conexion(f_name=f_name, l_name=l_name, address=e_address, phone=e_phone, ocupation=e_ocupation, email=e_email, category=e_category)
			db._add_user()
			self._hiddeshow(0)
			self._sensitive_options(1)
			self._load_contact(1)
			self._sensitive_confirmation(0)
			self.label_dialog.set_text("register saved succefull")
			self.pulse = 0
		elif self.pulse == 2:
			#pass
			db = conn_db.Conexion(self.id, f_name, l_name, e_address, e_phone, e_ocupation, e_email, e_category)	
			db._update_user()
			self._hiddeshow(0)
			self._sensitive_options(1)
			self._load_contact(1)
			self._sensitive_confirmation(0)
			self.label_dialog.set_text("register update succefull")
			self.pulse = 0
		else:
			#pass
			"""aqui paso nombre y apellido para extraer el id"""
			db = conn_db.Conexion(self.id)
			db._del_user()
			self._hiddeshow(0)
			self._sensitive_options(1)
			self._load_contact(1)
			self._sensitive_confirmation(0)
			self.label_dialog.set_text("delete succefull")
			self.pulse = 0

	def _aceptar(self, widget, data=None):
		if self.ef_name.get_text()!= "" and self.el_name.get_text()!="" and self.e_category.get_text()!="":
			f = self.ef_name.get_text().lower()
			l = self.el_name.get_text().lower()

			c = self.e_category.get_text().lower()
			name = f + " " + l

			db = conn_db.Conexion()
			if self.pulse == 1:
				if db._exists_user(name) or (c not in self.categorias):
					self.label_dialog.set_text("dont save contact on self-name or distinct cat")
				else:
					self._read_datas()
			elif self.pulse==2:
				#pass
				if (c not in self.categorias):
					self.label_dialog.set_text("dont save contact on distinct category")
				else:
					self.id = db._return_id(self.fname, self.lname)
					self._read_datas()
			else:
				#pass
				self.id = db._return_id(self.fname, self.lname)
				self._read_datas()				
		
	def _hiddeshow(self, i):
		if i:
			self.add.hide()
			self.update.hide()
			self.delete.hide()
			self.aceptar.show()
			self.cancelar.show()
		else:
			self.add.show()
			self.update.show()
			self.delete.show()
			self.aceptar.hide()
			self.cancelar.hide()
			
	def _cancelar(self, widget, data=None):
		self.pulse = 0
		self._sensitive_options(1)
		self._load_on_entry(self.display_now)
		self._sensitive_confirmation(0)
		self._hiddeshow(0)
		self.label_dialog.set_text("")

	def _add_user(self, widget, data=None):
		self._clear_entry()
		self.pic.set_from_file('./noavailable.png')
		self.pulse = 1
		self._sensitive_options(0)
		self._sensitive_confirmation(1)
		self._hiddeshow(1)
		self._editable_entry(1)

	def _update_user(self, widget, data=None):
		self.pulse = 2
		self.fname = self.ef_name.get_text().lower()
		self.lname = self.el_name.get_text().lower()
		self._editable_entry(True)
		self._sensitive_options(0)
		self._sensitive_confirmation(1)
		self._hiddeshow(1)

	def _del_user(self, widget, data=None):
		#pass
		self.pulse = 3
		self.fname = self.ef_name.get_text().lower()
		self.lname = self.el_name.get_text().lower()
		self._sensitive_options(0)
		self._sensitive_confirmation(1)
		self._hiddeshow(1)

	def _clear_entry(self):
		self.ef_name.set_text("")
		self.el_name.set_text("")
		self.e_address.set_text("")
		self.e_phone.set_text("")
		self.e_ocupation.set_text("")
		self.e_email.set_text("")
		self.e_category.set_text("")

	def _editable_entry(self, valor):
		if valor:
			self.ef_name.set_editable(True)
			self.el_name.set_editable(True)
			self.e_address.set_editable(True)
			self.e_phone.set_editable(True)
			self.e_ocupation.set_editable(True)
			self.e_email.set_editable(True)
			self.e_category.set_editable(True)
		else:
			self.ef_name.set_editable(False)
			self.el_name.set_editable(False)
			self.e_address.set_editable(False)
			self.e_phone.set_editable(False)
			self.e_ocupation.set_editable(False)
			self.e_email.set_editable(False)
			self.e_category.set_editable(False)

	def _delete_event(self, widget, event, data=None):
		gtk.main_quit()
		return False

	def main(self):
		gtk.main()
		return 0

if __name__=="__main__":
	c = Contactos()
	c.main()