XML Mill  1.0.0
A GUI based XML editor with a memory.
gcdbsessionmanager.cpp
00001 /* Copyright (c) 2012 - 2013 by William Hallatt.
00002  *
00003  * This file forms part of "XML Mill".
00004  *
00005  * The official website for this project is <http://www.goblincoding.com> and,
00006  * although not compulsory, it would be appreciated if all works of whatever
00007  * nature using this source code (in whole or in part) include a reference to
00008  * this site.
00009  *
00010  * Should you wish to contact me for whatever reason, please do so via:
00011  *
00012  *                 <http://www.goblincoding.com/contact>
00013  *
00014  * This program is free software: you can redistribute it and/or modify it under
00015  * the terms of the GNU General Public License as published by the Free Software
00016  * Foundation, either version 3 of the License, or (at your option) any later
00017  * version.
00018  *
00019  * This program is distributed in the hope that it will be useful, but WITHOUT
00020  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00021  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License along with
00024  * this program (GNUGPL.txt).  If not, see
00025  *
00026  *                    <http://www.gnu.org/licenses/>
00027  */
00028 
00029 #include "gcdbsessionmanager.h"
00030 #include "ui_gcdbsessionmanager.h"
00031 #include "db/gcdatabaseinterface.h"
00032 #include "utils/gcmessagespace.h"
00033 #include "utils/gcglobalspace.h"
00034 
00035 #include <QFileDialog>
00036 #include <QMessageBox>
00037 
00038 /*--------------------------------------------------------------------------------------*/
00039 
00040 GCDBSessionManager::GCDBSessionManager( QWidget* parent )
00041 : QDialog      ( parent ),
00042   ui           ( new Ui::GCDBSessionManager ),
00043   m_currentRoot( "" )
00044 {
00045   ui->setupUi( this );
00046 
00047   setDatabaseList();
00048   ui->showHelpButton->setVisible( false );
00049 
00050   connect( ui->addExistingButton, SIGNAL( clicked() ), this, SLOT( addExistingDatabase() ) );
00051   connect( ui->addNewButton, SIGNAL( clicked() ), this, SLOT( addNewDatabase() ) );
00052   connect( ui->cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
00053   connect( ui->okButton, SIGNAL( clicked() ), this, SLOT( setActiveDatabase() ) );
00054   connect( ui->okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
00055   connect( ui->showHelpButton, SIGNAL( clicked() ), this, SLOT( showHelp() ) );
00056 }
00057 
00058 /*--------------------------------------------------------------------------------------*/
00059 
00060 GCDBSessionManager::~GCDBSessionManager()
00061 {
00062   delete ui;
00063 }
00064 
00065 /*--------------------------------------------------------------------------------------*/
00066 
00067 void GCDBSessionManager::selectActiveDatabase( const QString& currentRoot )
00068 {
00069   /* Switching DB sessions while building an XML document could result in all kinds of trouble
00070     since the items known to the current session may not be known to the next. */
00071   m_currentRoot = currentRoot;
00072 
00073   this->setWindowTitle( "Select a profile for this session" );
00074   setDatabaseList();
00075 
00076   ui->addNewButton->setVisible( true );
00077   ui->addExistingButton->setVisible( true );
00078 
00079   disconnect( ui->okButton, SIGNAL( clicked() ), this, SLOT( removeDatabaseConnection() ) );
00080   connect( ui->okButton, SIGNAL( clicked() ), this, SLOT( setActiveDatabase() ), Qt::UniqueConnection );
00081 
00082   this->exec();
00083 }
00084 
00085 /*--------------------------------------------------------------------------------------*/
00086 
00087 void GCDBSessionManager::removeDatabase( const QString& currentRoot )
00088 {
00089   this->setWindowTitle( "Remove profile" );
00090 
00091   m_currentRoot = currentRoot;
00092   ui->addNewButton->setVisible( false );
00093   ui->addExistingButton->setVisible( false );
00094 
00095   disconnect( ui->okButton, SIGNAL( clicked() ), this, SLOT( setActiveDatabase() ) );
00096   connect( ui->okButton, SIGNAL( clicked() ), this, SLOT( removeDatabaseConnection() ), Qt::UniqueConnection );
00097 
00098   this->exec();
00099 }
00100 
00101 /*--------------------------------------------------------------------------------------*/
00102 
00103 void GCDBSessionManager::addExistingDatabase( const QString& currentRoot )
00104 {
00105   m_currentRoot = currentRoot;
00106 
00107   QString file = QFileDialog::getOpenFileName( this, "Add Existing Profile", GCGlobalSpace::lastUserSelectedDirectory(), "XML Profiles (*.db)" );
00108 
00109   /* If the user clicked "OK". */
00110   if( !file.isEmpty() )
00111   {
00112     addDatabaseConnection( file );
00113 
00114     /* Save the last visited directory. */
00115     QFileInfo fileInfo( file );
00116     QString finalDirectory = fileInfo.dir().path();
00117     GCGlobalSpace::setLastUserSelectedDirectory( finalDirectory );
00118   }
00119 }
00120 
00121 /*--------------------------------------------------------------------------------------*/
00122 
00123 void GCDBSessionManager::addNewDatabase( const QString& currentRoot )
00124 {
00125   m_currentRoot = currentRoot;
00126 
00127   QString file = QFileDialog::getSaveFileName( this, "Add New Profile", GCGlobalSpace::lastUserSelectedDirectory(), "XML Profiles (*.db)" );
00128 
00129   /* If the user clicked "OK". */
00130   if( !file.isEmpty() )
00131   {
00132     addDatabaseConnection( file );
00133 
00134     /* Save the last visited directory. */
00135     QFileInfo fileInfo( file );
00136     QString finalDirectory = fileInfo.dir().path();
00137     GCGlobalSpace::setLastUserSelectedDirectory( finalDirectory );
00138   }
00139 }
00140 
00141 /*--------------------------------------------------------------------------------------*/
00142 
00143 void GCDBSessionManager::removeDatabaseConnection()
00144 {
00145   QString dbName = ui->comboBox->currentText();
00146 
00147   if( !m_currentRoot.isEmpty() &&
00148       GCDataBaseInterface::instance()->activeSessionName() == dbName )
00149   {
00150     bool accepted = GCMessageSpace::userAccepted( "RemoveActiveSessionWarning",
00151                                                   "Warning!",
00152                                                   "Removing the active profile will cause the current "
00153                                                   "document to be reset and your work will be lost! ",
00154                                                   GCMessageSpace::OKCancel,
00155                                                   GCMessageSpace::Cancel,
00156                                                   GCMessageSpace::Warning );
00157 
00158     if( !accepted )
00159     {
00160       return;
00161     }
00162   }
00163 
00164   if( !GCDataBaseInterface::instance()->removeDatabase( dbName ) )
00165   {
00166     QString error = QString( "Failed to remove profile \"%1\": [%2]" ).arg( dbName )
00167       .arg( GCDataBaseInterface::instance()->lastError() );
00168     GCMessageSpace::showErrorMessageBox( this, error );
00169   }
00170 }
00171 
00172 /*--------------------------------------------------------------------------------------*/
00173 
00174 void GCDBSessionManager::setActiveDatabase()
00175 {
00176   setActiveDatabase( ui->comboBox->currentText() );
00177 }
00178 
00179 /*--------------------------------------------------------------------------------------*/
00180 
00181 void GCDBSessionManager::showHelp()
00182 {
00183   QMessageBox::information( this,
00184                             "How this works...",
00185                             "Profiles are used to store information about XML files, so please: \n\n"
00186                             "1. Create a new profile for the type of XML you are going to work with (I suggest "
00187                             "using a name that closely resembles the XML files it will represent), or\n"
00188                             "2. Add an existing profile(s) if you have any." );
00189 }
00190 
00191 /*--------------------------------------------------------------------------------------*/
00192 
00193 void GCDBSessionManager::setActiveDatabase( const QString& dbName )
00194 {
00195   /* If the current root element is not known to the new session, the user must
00196     confirm whether or not he/she wants the active document to be reset. */
00197   if( !m_currentRoot.isEmpty() &&
00198       !GCDataBaseInterface::instance()->containsKnownRootElement( dbName, m_currentRoot ) )
00199   {
00200     QMessageBox::StandardButton accept = QMessageBox::question( this,
00201                                                                 "Unsupported document",
00202                                                                 "The selected profile doesn't support your current document. Your "
00203                                                                 "document will be reset if you continue.",
00204                                                                 QMessageBox::Ok | QMessageBox::Cancel,
00205                                                                 QMessageBox::Cancel );
00206     if( accept != QMessageBox::Ok )
00207     {
00208       return;
00209     }
00210 
00211     emit reset();
00212     m_currentRoot = "";
00213   }
00214 
00215   if( !GCDataBaseInterface::instance()->setActiveDatabase( dbName ) )
00216   {
00217     QString error = QString( "Failed to set session \"%1\" as active - [%2]" ).arg( dbName )
00218       .arg( GCDataBaseInterface::instance()->lastError() );
00219     GCMessageSpace::showErrorMessageBox( this, error );
00220   }
00221   else
00222   {
00223     this->hide();
00224     this->accept();
00225     emit activeDatabaseChanged( dbName );
00226   }
00227 }
00228 
00229 /*--------------------------------------------------------------------------------------*/
00230 
00231 void GCDBSessionManager::addDatabaseConnection( const QString& dbName )
00232 {
00233   if( !GCDataBaseInterface::instance()->addDatabase( dbName ) )
00234   {
00235     GCMessageSpace::showErrorMessageBox( this, GCDataBaseInterface::instance()->lastError() );
00236     return;
00237   }
00238 
00239   bool accepted = GCMessageSpace::userAccepted( "SwitchActiveSession",
00240                                                 "Switch Session",
00241                                                 "Would you like to switch to the new profile?",
00242                                                 GCMessageSpace::YesNo,
00243                                                 GCMessageSpace::Yes,
00244                                                 GCMessageSpace::Question );
00245 
00246   if( accepted )
00247   {
00248     setActiveDatabase( dbName );
00249   }
00250 
00251   setDatabaseList();
00252 }
00253 
00254 /*--------------------------------------------------------------------------------------*/
00255 
00256 void GCDBSessionManager::setDatabaseList()
00257 {
00258   ui->comboBox->clear();
00259 
00260   QStringList dbList = GCDataBaseInterface::instance()->connectionList();
00261 
00262   if( dbList.empty() )
00263   {
00264     ui->okButton->setVisible( false );
00265     ui->comboBox->addItem( "You don't have any known profiles..." );
00266     ui->showHelpButton->setVisible( true );
00267   }
00268   else
00269   {
00270     ui->okButton->setVisible( true );
00271     ui->comboBox->addItems( dbList );
00272     ui->showHelpButton->setVisible( false );
00273   }
00274 }
00275 
00276 /*--------------------------------------------------------------------------------------*/