00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 import base64
00029 from PyQt4.QtCore import *
00030 from PyQt4.QtGui import *
00031 from PyQt4.uic import *
00032 import ServerConfigurationDialog
00033 from Koo.Common import Common
00034 from Koo.Common.Settings import *
00035 
00036 (DatabaseDialogUi, DatabaseDialogBase) = loadUiType(  Common.uiPath('choosedb.ui') )
00037 
00038 class DatabaseDialog( QDialog, DatabaseDialogUi ):
00039         
00040         
00041         
00042         TypeSelect = 1
00043         TypeEdit   = 2
00044 
00045         def __init__(self, type, title, parent=None):
00046                 QDialog.__init__(self, parent)
00047                 DatabaseDialogUi.__init__(self)
00048                 self.setupUi( self )
00049 
00050                 self.type = type
00051                 if type == DatabaseDialog.TypeSelect:
00052                         self.uiDatabaseEditor.setVisible( False )
00053                         self.uiDatabaseLabel.setBuddy( self.uiDatabaseSelector )
00054                 else:
00055                         self.uiDatabaseSelector.setVisible( False )
00056                         self.uiDatabaseLabel.setBuddy( self.uiDatabaseEditor )
00057 
00058                 url = QUrl( Settings.value('login.url') )
00059                 url.setUserName( '' )
00060                 self.uiServer.setText( url.toString() )
00061 
00062                 self.uiTitle.setText( title )
00063                 self.setModal( True )
00064                 self.uiInformation.setVisible( False )
00065                 self.connect( self.pushChange, SIGNAL('clicked()'), self.slotChange )
00066                 self.connect( self.pushAccept, SIGNAL('clicked()'), self.slotAccept )
00067                 self.connect( self.pushCancel, SIGNAL('clicked()'), self.reject )
00068                 self.refreshList()
00069         
00070         def refreshList(self):
00071                 res = ServerConfigurationDialog.refreshDatabaseList(self.uiDatabaseSelector, str( self.uiServer.text() ) )
00072                 if res == -1:
00073                         self.uiInformation.setText('<b>'+_('Could not connect to server !')+'</b>')
00074                         self.uiInformation.setVisible( True )
00075                         self.uiDatabaseSelector.setEnabled( False )
00076                         self.uiDatabaseEditor.setEnabled( False )
00077                         self.pushAccept.setEnabled( False )
00078                 elif res==0:
00079                         self.uiInformation.setText('<b>'+_('No database found, you must create one !')+'</b>')
00080                         self.uiInformation.setVisible( True )
00081                         self.uiDatabaseSelector.setEnabled( False )
00082                         self.uiDatabaseEditor.setEnabled( False )
00083                         self.pushAccept.setEnabled( False )
00084                 else:
00085                         self.uiInformation.setVisible( False )
00086                         self.uiDatabaseSelector.setEnabled( True )
00087                         self.uiDatabaseEditor.setEnabled( True )
00088                         self.pushAccept.setEnabled( True )
00089                 return res
00090         
00091         def slotAccept(self):
00092                 self.url = str( self.uiServer.text() )
00093                 if self.type == DatabaseDialog.TypeSelect:
00094                         self.databaseName = str( self.uiDatabaseSelector.currentText() )
00095                 else:
00096                         self.databaseName = str( self.uiDatabaseEditor.text() )
00097                 self.password = str( self.uiPassword.text() )
00098                 self.accept()
00099                 
00100         def slotChange(self):
00101                 dialog = ServerConfigurationDialog.ServerConfigurationDialog(self)
00102                 dialog.setUrl( Settings.value( 'login.url' ) )
00103                 if dialog.exec_() == QDialog.Accepted:
00104                         self.uiServer.setText( dialog.url )
00105                         self.refreshList()
00106 
00107 def restoreDatabase(parent):
00108         fileName = QFileDialog.getOpenFileName(parent, _('Open backup file...') )
00109         if fileName.isNull():
00110                 return
00111         dialog = DatabaseDialog( DatabaseDialog.TypeEdit, _('Restore a database'), parent)
00112         r = dialog.exec_()
00113         if r == QDialog.Rejected:
00114                 return
00115         parent.setCursor( Qt.WaitCursor )
00116         try:
00117                 f = file(fileName, 'rb')
00118                 data = base64.encodestring(f.read())
00119                 f.close()
00120                 Rpc.database.call(dialog.url, 'restore', dialog.password, dialog.databaseName, data)
00121         except Exception,e:
00122                 parent.unsetCursor()
00123                 if e.message=='AccessDenied:None':
00124                         QMessageBox.warning( parent, _('Could not restore database'), _('Bad database administrator password!') )
00125                 else:
00126                         QMessageBox.warning( parent, _('Could not restore database'), _('There was an error restoring the database!') )
00127                 return
00128         parent.unsetCursor()
00129         QMessageBox.information( parent, _('Information'), _('Database restored successfully!') )
00130 
00131 def backupDatabase(parent):
00132         dialog = DatabaseDialog( DatabaseDialog.TypeSelect, _('Backup a database'), parent )
00133         r = dialog.exec_()
00134         if r == QDialog.Rejected:
00135                 return
00136         fileName = QFileDialog.getSaveFileName( parent, _('Save as...') )
00137         if fileName.isNull():
00138                 return
00139         parent.setCursor( Qt.WaitCursor )
00140         try:
00141                 dump_b64 = Rpc.database.execute(dialog.url, 'dump', dialog.password, dialog.databaseName)
00142                 dump = base64.decodestring(dump_b64)
00143                 f = file(fileName, 'wb')
00144                 f.write(dump)
00145                 f.close()
00146         except Exception, e:
00147                 parent.unsetCursor()
00148                 QMessageBox.warning( parent, _('Error'), _('Could not backup database.\n%s') % (str(e)) )
00149                 return
00150         parent.unsetCursor()
00151         QMessageBox.information( parent, _('Information'), _("Database backuped successfully!"))
00152 
00153 def dropDatabase(parent):
00154         dialog = DatabaseDialog( DatabaseDialog.TypeSelect, _('Delete a database'), parent )
00155         r = dialog.exec_()
00156 
00157         if r == QDialog.Rejected:
00158                 return
00159         parent.setCursor( Qt.WaitCursor )
00160         try:
00161                 Rpc.database.call(dialog.url, 'drop', dialog.password, dialog.databaseName )
00162         except Exception, e:
00163                 parent.unsetCursor()
00164                 if e.message=='AccessDenied:None':
00165                         QMessageBox.warning( parent, _("Could not drop database."), _('Bad database administrator password !') )
00166                 else:
00167                         QMessageBox.warning( parent, _("Database removal"), _("Couldn't drop database") )
00168                 return
00169         parent.unsetCursor()
00170         QMessageBox.information( parent, _('Database removal'), _('Database dropped successfully!') )
00171