too many changes to remember

Signed-off-by: fpdb-mme <jUrner@arcor.de>
This commit is contained in:
fpdb-mme 2009-10-27 11:04:24 +01:00
parent 30103c5cb2
commit e732b74956

View File

@ -28,43 +28,31 @@ class DatabaseTypeBase(object):
__metaclass__ = DatabaseTypeMeta __metaclass__ = DatabaseTypeMeta
Type = None Type = None
DBHasHost = 0x1 class DatabaseTypePostgres(DatabaseTypeBase):
DBHasFile = 0x2 Type = 'postgres'
DBHasPort = 0x4 def __init__(self, host='localhost', port=5432, user='postgres', password='', database='fpdb', name=''):
DBHasUser = 0x8
DBHasPassword = 0x10
DBHasDatabase = 0x20
DBHasName = 0x40
DBFlagsFileSystem = DBHasFile|DBHasName
DBFlagsServer = DBHasHost|DBHasPort|DBHasUser|DBHasPassword|DBHasDatabase|DBHasName
def __init__(self, host='', file='', port=0, user='', password='', database='', table='', name=''):
self.host = host self.host = host
self.file = file
self.port = port self.port = port
self.user = user self.user = user
self.password = password self.password = password
self.database = database self.database = database
self.name = name self.name = name
class DatabaseTypePostgres(DatabaseTypeBase):
Type = 'postgres'
Flags = DatabaseTypeBase.DBFlagsServer
def __init__(self, host='localhost', file='', port=5432, user='postgres', password='', database='fpdb', name=''):
DatabaseTypeBase.__init__(self, host=host, file=file, port=port, user=user, password=password, database=database, name=name)
class DatabaseTypeMysql(DatabaseTypeBase): class DatabaseTypeMysql(DatabaseTypeBase):
Type = 'mysql' Type = 'mysql'
Flags = DatabaseTypeBase.DBFlagsServer def __init__(self, host='localhost', port=3306, user='root', password='', database='fpdb', name=''):
def __init__(self, host='localhost', file='root', port=3306, user='', password='', database='fpdb', name=''): self.host = host
DatabaseTypeBase.__init__(self, host=host, file=file, port=port, user=user, password=password, database=database, name=name) self.port = port
self.user = user
self.password = password
self.database = database
self.name = name
class DatabaseTypeSqLite(DatabaseTypeBase): class DatabaseTypeSqLite(DatabaseTypeBase):
Type = 'sqlite' Type = 'sqlite'
Flags = DatabaseTypeBase.DBFlagsFileSystem def __init__(self, host='', file='', name=''):
def __init__(self, host='', file='', port=0, user='', password='',database='', name=''): self.file = file
DatabaseTypeBase.__init__(self, host=host, file=file, port=port, user=user, password=password, database=database, name=name) self.name = name
#*************************************************************************************************************************** #***************************************************************************************************************************
class MyFileChooserButton(gtk.HBox): class MyFileChooserButton(gtk.HBox):
@ -124,99 +112,121 @@ class MyFileChooserButton(gtk.HBox):
# #
class DialogDatabaseProperties(gtk.Dialog):
def __init__(self, databaseManager, database=None,parent=None):
gtk.Dialog.__init__(self,
title="My dialog",
parent=parent,
flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
buttons=(
gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT,
)
)
self.connect('response', self.on_dialog_response)
# setup widget
self.widgetDatabaseProperties = WidgetDatabaseProperties(databaseManager,database=database)
self.vbox.pack_start(self.widgetDatabaseProperties, True, True)
self.widgetDatabaseProperties.show_all()
def get_database(self):
return self.widgetDatabaseProperties.get_database()
def on_dialog_response(self, dlg, responseId):
if responseId == gtk.RESPONSE_REJECT:
pass
elif responseId == gtk.RESPONSE_ACCEPT:
pass
class WidgetDatabaseProperties(gtk.VBox): class WidgetDatabaseProperties(gtk.VBox):
def __init__(self, databaseManager, database=None): ModeEdit = 0x1
ModeAdd = 0x2
ModeNew = 0x4
class FieldWidget(object):
def __init__(self, text='', attrDatabase='', widget=None, attrGet=None, attrSet=None, defaultValue=None, canEdit=False, tooltip=''):
"""
@param canEdit: True if the user can edit the attr in edit mode, False otherwise
"""
self._label = gtk.Label(text)
self._attrDatabase = attrDatabase
self._widget = widget
self._defaultValue = defaultValue
self._attrGetter=None,
self._attrGet = attrGet
self._attrSet = attrSet
self._canEdit = canEdit
self._label.set_tooltip_text(tooltip)
self._widget.set_tooltip_text(tooltip)
def widget(self):
return self._widget
def label(self):
return self._label
def is_sensitive(self, database):
return hasattr(database, self._attrDatabase)
def can_edit(self):
return self._canEdit
def set_sensitive(self, flag):
self._label.set_sensitive(flag)
self._widget.set_sensitive(flag)
def value_from_database(self, database):
getattr(self._widget, self._attrSet)( getattr(database, self._attrDatabase) )
def value_to_database(self, database):
setattr(database, self._attrDatabase, getattr(self._widget, self._attrGet)() )
def reset_value(self):
getattr(self._widget, self._attrSet)(self._defaultValue)
def __init__(self, databaseManager, database=None, flags=ModeEdit):
gtk.VBox.__init__(self) gtk.VBox.__init__(self)
self.flags = flags
self.fieldWidgets = ( #fieldName--> fieldHandler self.fieldWidgets = ( #fieldName--> fieldHandler
{ self.FieldWidget(
'label': gtk.Label('Name:'), text='Name:',
'widget': gtk.Entry(), attrDatabase='name',
'getter': lambda widget, database: setattr(database, 'name', widget.get_text() ), widget=gtk.Entry(),
'setter': lambda widget, database: widget.set_text(database.name), defaultValue='',
'isSensitive': lambda database: bool(database.Flags & database.DBHasName), attrGet='get_text',
'tooltip': '', attrSet='set_text',
}, canEdit=True,
{ tooltip=''
'label': gtk.Label('File:'), ),
'widget': MyFileChooserButton(), self.FieldWidget(
'getter': lambda widget, database: lambda widget, database: setattr(database, 'file', widget.get_filename() ), text='File:',
'setter': lambda widget, database: widget.set_filename(database.file), attrDatabase='file',
'isSensitive': lambda database: bool(database.Flags & database.DBHasFile), widget=MyFileChooserButton(),
'tooltip': '', defaultValue='',
}, attrGet='get_filename',
{ attrSet='set_filename',
'label': gtk.Label('Host:'), canEdit=False,
'widget': gtk.Entry(), tooltip=''
'getter': lambda widget, database: setattr(database, 'host', widget.get_text() ), ),
'setter': lambda widget, database: widget.set_text(database.host), self.FieldWidget(
'isSensitive': lambda database: bool(database.Flags & database.DBHasHost), text='Host:',
'tooltip': '', attrDatabase='host',
}, widget=gtk.Entry(),
{ defaultValue='',
'label': gtk.Label('Port:'), attrGet='get_text',
'widget': gtk.SpinButton(adjustment=gtk.Adjustment(value=0, lower=0, upper=999999, step_incr=1, page_incr=10) ), attrSet='set_text',
'getter': lambda widget, database: setattr(database, 'port', widget.get_value() ), canEdit=False,
'setter': lambda widget, database: widget.set_value(database.port), tooltip=''
'isSensitive': lambda database: bool(database.Flags & database.DBHasPort), ),
'tooltip': '', self.FieldWidget(
}, text='Port:',
{ attrDatabase='port',
'label': gtk.Label('User:'), widget=gtk.SpinButton(adjustment=gtk.Adjustment(value=0, lower=0, upper=999999, step_incr=1, page_incr=10) ),
'widget': gtk.Entry(), defaultValue=0,
'getter': lambda widget, database: setattr(database, 'user', widget.get_text() ), attrGet='get_value',
'setter': lambda widget, database: widget.set_text(database.user), attrSet='set_value',
'isSensitive': lambda database: bool(database.Flags & database.DBHasUser), canEdit=False,
'tooltip': '', tooltip=''
}, ),
{ self.FieldWidget(
'label': gtk.Label('Pwd:'), text='User:',
'widget': gtk.Entry(), attrDatabase='user',
'getter': lambda widget, database: setattr(database, 'password', widget.get_text() ), widget=gtk.Entry(),
'setter': lambda widget, database: widget.set_text(database.password), defaultValue='',
'isSensitive': lambda database: bool(database.Flags & database.DBHasPassword), attrGet='get_text',
'tooltip': '', attrSet='set_text',
}, canEdit=False,
{ tooltip=''
'label': gtk.Label('DB:'), ),
'widget': gtk.Entry(), self.FieldWidget(
'getter': lambda widget, database: setattr(database, 'database', widget.get_text() ), text='Pwd:',
'setter': lambda widget, database: widget.set_text(database.database), attrDatabase='password',
'isSensitive': lambda database: bool(database.Flags & database.DBHasDatabase), widget=gtk.Entry(),
'tooltip': 'enter name of the database to create', defaultValue='',
}, attrGet='get_text',
attrSet='set_text',
canEdit=False,
tooltip=''
),
self.FieldWidget(
text='Db:',
attrDatabase='database',
widget=gtk.Entry(),
defaultValue='',
attrGet='get_text',
attrSet='set_text',
canEdit=False,
tooltip=''
),
) )
# setup database type combo # setup database type combo
@ -236,10 +246,8 @@ class WidgetDatabaseProperties(gtk.VBox):
table = gtk.Table(rows=len(self.fieldWidgets) +1, columns=2, homogeneous=False) table = gtk.Table(rows=len(self.fieldWidgets) +1, columns=2, homogeneous=False)
self.pack_start(table, False, False, 2) self.pack_start(table, False, False, 2)
for i,fieldWidget in enumerate(self.fieldWidgets): for i,fieldWidget in enumerate(self.fieldWidgets):
fieldWidget['widget'].set_tooltip_text(fieldWidget['tooltip']) table.attach(fieldWidget.label(), 0, 1, i, i+1, xoptions=gtk.FILL)
table.attach(fieldWidget.widget(), 1, 2, i, i+1)
table.attach(fieldWidget['label'], 0, 1, i, i+1, xoptions=gtk.FILL)
table.attach(fieldWidget['widget'], 1, 2, i, i+1)
# init widget # init widget
@ -287,17 +295,60 @@ class WidgetDatabaseProperties(gtk.VBox):
# adjust field widgets to database # adjust field widgets to database
for fieldWidget in self.fieldWidgets: for fieldWidget in self.fieldWidgets:
isSensitive = fieldWidget['isSensitive'](self.database) isSensitive = fieldWidget.is_sensitive(self.database)
fieldWidget['widget'].set_sensitive(isSensitive) if isSensitive:
fieldWidget['label'].set_sensitive(isSensitive) fieldWidget.value_from_database(self.database)
fieldWidget['setter'](fieldWidget['widget'], self.database) else:
fieldWidget.reset_value()
if self.flags & self.ModeEdit:
isSensitive = isSensitive and fieldWidget.can_edit()
fieldWidget.set_sensitive(isSensitive)
def get_database(self): def get_database(self):
for fieldWidget in self.fieldWidgets: for fieldWidget in self.fieldWidgets:
fieldWidget['getter'](fieldWidget['widget'], self.database) if fieldWidget.is_sensitive(self.database):
fieldWidget.value_to_database(self.database)
return self.database return self.database
class DialogDatabaseProperties(gtk.Dialog):
def __init__(self, databaseManager, database=None,parent=None, flags=WidgetDatabaseProperties.ModeEdit):
if flags & WidgetDatabaseProperties.ModeEdit:
title = '[Edit database] - database properties'
elif flags & WidgetDatabaseProperties.ModeAdd:
title = '[Add database] - database properties'
elif flags & WidgetDatabaseProperties.ModeNew:
title = '[New database] - database properties'
else:
title = 'database properties'
gtk.Dialog.__init__(self,
title=title,
parent=parent,
flags=gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
buttons=(
gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
gtk.STOCK_OK, gtk.RESPONSE_ACCEPT,
)
)
self.connect('response', self.on_dialog_response)
# setup widget
self.flags = flags
self.widgetDatabaseProperties = WidgetDatabaseProperties(databaseManager,database=database, flags=self.flags)
self.vbox.pack_start(self.widgetDatabaseProperties, True, True)
self.widgetDatabaseProperties.show_all()
def get_database(self):
return self.widgetDatabaseProperties.get_database()
def on_dialog_response(self, dlg, responseId):
if responseId == gtk.RESPONSE_REJECT:
pass
elif responseId == gtk.RESPONSE_ACCEPT:
pass
#TODO: just boilerplate code #TODO: just boilerplate code
class DialogDatabase(gtk.Dialog): class DialogDatabase(gtk.Dialog):
def __init__(self, databaseManager, parent=None): def __init__(self, databaseManager, parent=None):
@ -332,13 +383,16 @@ class DialogDatabase(gtk.Dialog):
self.buttonDatabaseAdd.connect('clicked', self.on_button_database_add_clicked) self.buttonDatabaseAdd.connect('clicked', self.on_button_database_add_clicked)
self.buttonDatabaseEdit = gtk.Button("Edit..") self.buttonDatabaseEdit = gtk.Button("Edit..")
self.buttonDatabaseEdit.set_tooltip_text('edit database settings') self.buttonDatabaseEdit.set_tooltip_text('edit database settings')
#self.buttonDatabaseEdit.connect('clicked', self.on_button_database_edit_clicked) self.buttonDatabaseEdit.connect('clicked', self.on_button_database_edit_clicked)
self.buttonDatabaseEdit.set_sensitive(False)
self.buttonDatabaseRemove = gtk.Button("Remove") self.buttonDatabaseRemove = gtk.Button("Remove")
self.buttonDatabaseRemove.set_tooltip_text('removes the database from the list') self.buttonDatabaseRemove.set_tooltip_text('removes the database from the list')
self.buttonDatabaseRemove.set_sensitive(False)
#TODO: i dont think we should do any real database management here. maybe drop it #TODO: i dont think we should do any real database management here. maybe drop it
self.buttonDatabaseDelete = gtk.Button("Delete") self.buttonDatabaseDelete = gtk.Button("Delete")
self.buttonDatabaseDelete.set_tooltip_text('removes the database from the list and deletes it') self.buttonDatabaseDelete.set_tooltip_text('removes the database from the list and deletes it')
self.buttonDatabaseDelete.set_sensitive(False)
# database tree # database tree
self.treeDatabases = gtk.TreeView() self.treeDatabases = gtk.TreeView()
@ -348,6 +402,7 @@ class DialogDatabase(gtk.Dialog):
for i, column in enumerate(columns): for i, column in enumerate(columns):
col = gtk.TreeViewColumn(column, gtk.CellRendererText(), text=i) col = gtk.TreeViewColumn(column, gtk.CellRendererText(), text=i)
self.treeDatabases.append_column(col) self.treeDatabases.append_column(col)
self.treeDatabases.get_selection().connect('changed', self.on_tree_databases_selection_changed)
# layout widgets # layout widgets
self.vbox.pack_start(self.labelInfo, False, False, 2) self.vbox.pack_start(self.labelInfo, False, False, 2)
@ -374,34 +429,44 @@ class DialogDatabase(gtk.Dialog):
#TODO: for some reason i have to click OK/Cancel twice to close the dialog #TODO: for some reason i have to click OK/Cancel twice to close the dialog
def on_button_database_new_clicked(self, button): def on_button_database_new_clicked(self, button):
dlg = DialogDatabaseProperties(self.databaseManager, parent=self) dlg = DialogDatabaseProperties(self.databaseManager, parent=self, flags=WidgetDatabaseProperties.ModeNew)
if dlg.run() == gtk.RESPONSE_REJECT: if dlg.run() == gtk.RESPONSE_REJECT:
pass pass
if dlg.run() == gtk.RESPONSE_ACCEPT: if dlg.run() == gtk.RESPONSE_ACCEPT:
database = dlg.get_database() database = dlg.get_database()
self.treeDatabases.get_model().append( (database.name, 'foo', database.Type) ) self.treeDatabases.get_model().append( (database.name, 'foo', database.Type) )
dlg.destroy() dlg.destroy()
def on_button_database_add_clicked(self, button): def on_button_database_add_clicked(self, button):
dlg = DialogDatabaseProperties(self.databaseManager, parent=self) dlg = DialogDatabaseProperties(self.databaseManager, parent=self, flags=WidgetDatabaseProperties.ModeAdd)
if dlg.run() == gtk.RESPONSE_REJECT: if dlg.run() == gtk.RESPONSE_REJECT:
pass pass
if dlg.run() == gtk.RESPONSE_ACCEPT: if dlg.run() == gtk.RESPONSE_ACCEPT:
database = dlg.get_database() database = dlg.get_database()
self.treeDatabases.get_model().append( (database.name, 'foo', database.Type) ) self.treeDatabases.get_model().append( (database.name, 'foo', database.Type) )
dlg.destroy() dlg.destroy()
#def on_button_database_edit_clicked(self, button): def on_button_database_edit_clicked(self, button):
# dlg = DialogDatabaseProperties(self.databaseManager, parent=self) dlg = DialogDatabaseProperties(self.databaseManager, parent=self, flags=WidgetDatabaseProperties.ModeEdit)
# if dlg.run() == gtk.RESPONSE_REJECT: if dlg.run() == gtk.RESPONSE_REJECT:
# pass pass
# if dlg.run() == gtk.RESPONSE_ACCEPT: if dlg.run() == gtk.RESPONSE_ACCEPT:
# database = dlg.get_database() database = dlg.get_database()
# self.treeDatabases.get_model().append( (database.name, 'foo', database.Type) ) selection = self.treeDatabases.get_selection()
# if selection is not None:
# dlg.destroy() model, iter = selection.get_selected()
model.set_value(iter, 0, database.name)
dlg.destroy()
def on_tree_databases_selection_changed(self, treeSelection):
hasSelection = bool(treeSelection.count_selected_rows())
# enable/disable selection dependend widgets
self.buttonDatabaseEdit.set_sensitive(hasSelection)
self.buttonDatabaseRemove.set_sensitive(hasSelection)
self.buttonDatabaseDelete.set_sensitive(hasSelection)
#************************************************************************************************** #**************************************************************************************************