kexi

kexitablepart.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003 Lucijan Busch <lucijan@kde.org>
00003    Copyright (C) 2002, 2003 Joseph Wenninger <jowenn@kde.org>
00004    Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>
00005 
00006    This library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public
00008    License as published by the Free Software Foundation; either
00009    version 2 of the License, or (at your option) any later version.
00010 
00011    This library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public License
00017    along with this library; see the file COPYING.LIB.  If not, write to
00018    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include "kexitablepart.h"
00023 
00024 #include <kdebug.h>
00025 #include <kgenericfactory.h>
00026 #include <kmessagebox.h>
00027 #include <ktabwidget.h>
00028 #include <kiconloader.h>
00029 
00030 #include "keximainwindow.h"
00031 #include "kexiproject.h"
00032 #include "kexipartinfo.h"
00033 #include "widget/kexidatatable.h"
00034 #include "widget/tableview/kexidatatableview.h"
00035 #include "kexitabledesignerview.h"
00036 #include "kexitabledesigner_dataview.h"
00037 #include "kexilookupcolumnpage.h"
00038 
00039 #include <kexidb/connection.h>
00040 #include <kexidb/cursor.h>
00041 #include <kexidialogbase.h>
00042 
00044 class KexiTablePart::Private
00045 {
00046     public:
00047         Private()
00048         {
00049         }
00050         ~Private()
00051         {
00052             delete static_cast<KexiLookupColumnPage*>(lookupColumnPage);
00053         }
00054         QGuardedPtr<KexiLookupColumnPage> lookupColumnPage;
00055 };
00056 
00057 KexiTablePart::KexiTablePart(QObject *parent, const char *name, const QStringList &l)
00058  : KexiPart::Part(parent, name, l)
00059  , d(new Private())
00060 {
00061     // REGISTERED ID:
00062     m_registeredPartID = (int)KexiPart::TableObjectType;
00063 
00064     kdDebug() << "KexiTablePart::KexiTablePart()" << endl;
00065     m_names["instanceName"] 
00066         = i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
00067         "Use '_' character instead of spaces. First character should be a..z character. "
00068         "If you cannot use latin characters in your language, use english word.", 
00069         "table");
00070     m_names["instanceCaption"] = i18n("Table");
00071     m_supportedViewModes = Kexi::DataViewMode | Kexi::DesignViewMode;
00072 //js TODO: also add Kexi::TextViewMode when we'll have SQL ALTER TABLE EDITOR!!!
00073 }
00074 
00075 KexiTablePart::~KexiTablePart()
00076 {
00077     delete d;
00078 }
00079 
00080 void KexiTablePart::initPartActions()
00081 {
00082 }
00083 
00084 void KexiTablePart::initInstanceActions()
00085 {
00086 //moved to main window  createSharedAction(Kexi::DataViewMode, i18n("Filter"), "filter", 0, "tablepart_filter");
00087 
00088     KAction *a = createSharedToggleAction(
00089         Kexi::DesignViewMode, i18n("Primary Key"), "key", 0, "tablepart_toggle_pkey");
00090 //      Kexi::DesignViewMode, i18n("Toggle Primary Key"), "key", 0, "tablepart_toggle_pkey");
00091     a->setWhatsThis(i18n("Sets or removes primary key for currently selected field."));
00092 }
00093 
00094 KexiDialogTempData* KexiTablePart::createTempData(KexiDialogBase* dialog)
00095 {
00096     return new KexiTablePart::TempData(dialog);
00097 }
00098 
00099 KexiViewBase* KexiTablePart::createView(QWidget *parent, KexiDialogBase* dialog, 
00100     KexiPart::Item &item, int viewMode, QMap<QString,QString>*)
00101 {
00102     KexiMainWindow *win = dialog->mainWin();
00103     if (!win || !win->project() || !win->project()->dbConnection())
00104         return 0;
00105 
00106 
00107     KexiTablePart::TempData *temp = static_cast<KexiTablePart::TempData*>(dialog->tempData());
00108     if (!temp->table) {
00109         temp->table = win->project()->dbConnection()->tableSchema(item.name());
00110         kdDebug() << "KexiTablePart::execute(): schema is " << temp->table << endl;
00111     }
00112 
00113     if (viewMode == Kexi::DesignViewMode) {
00114         KexiTableDesignerView *t = new KexiTableDesignerView(win, parent);
00115         return t;
00116     }
00117     else if (viewMode == Kexi::DataViewMode) {
00118         if(!temp->table)
00119             return 0; //todo: message
00120         //we're not setting table schema here -it will be forced to set 
00121         // in KexiTableDesigner_DataView::afterSwitchFrom()
00122         KexiTableDesigner_DataView *t = new KexiTableDesigner_DataView(win, parent);
00123         return t;
00124     }
00125     return 0;
00126 }
00127 
00128 bool KexiTablePart::remove(KexiMainWindow *win, KexiPart::Item &item)
00129 {
00130     if (!win || !win->project() || !win->project()->dbConnection())
00131         return false;
00132 
00133     KexiDB::Connection *conn = win->project()->dbConnection();
00134     KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());
00135 
00136     if (sch) {
00137         tristate res = KexiTablePart::askForClosingObjectsUsingTableSchema(
00138             win, *conn, *sch, 
00139             i18n("You are about to remove table \"%1\" but following objects using this table are opened:")
00140             .arg(sch->name()));
00141         return true == conn->dropTable( sch );
00142     }
00143     //last chance: just remove item
00144     return conn->removeObject( item.identifier() );
00145 }
00146 
00147 tristate KexiTablePart::rename(KexiMainWindow *win, KexiPart::Item & item, 
00148     const QString& newName)
00149 {
00150 //TODO: what about objects (queries/forms) that use old name?
00151     KexiDB::Connection *conn = win->project()->dbConnection();
00152     KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());
00153     if (!sch)
00154         return false;
00155     return conn->alterTableName(*sch, newName);
00156 }
00157 
00158 KexiDB::SchemaData*
00159 KexiTablePart::loadSchemaData(KexiDialogBase *dlg, const KexiDB::SchemaData& sdata, int viewMode)
00160 {
00161     Q_UNUSED( viewMode );
00162 
00163     return dlg->mainWin()->project()->dbConnection()->tableSchema( sdata.name() );
00164 }
00165 
00166 #if 0
00167 KexiPart::DataSource *
00168 KexiTablePart::dataSource()
00169 {
00170     return new KexiTableDataSource(this);
00171 }
00172 #endif
00173 
00174 tristate KexiTablePart::askForClosingObjectsUsingTableSchema(QWidget *parent, KexiDB::Connection& conn, 
00175     KexiDB::TableSchema& table, const QString& msg)
00176 {
00177     QPtrList<KexiDB::Connection::TableSchemaChangeListenerInterface>* listeners
00178         = conn.tableSchemaChangeListeners(table);
00179     if (!listeners || listeners->isEmpty())
00180         return true;
00181     
00182     QString openedObjectsStr = "<ul>";
00183     for (QPtrListIterator<KexiDB::Connection::TableSchemaChangeListenerInterface> it(*listeners);
00184         it.current(); ++it) {
00185             openedObjectsStr += QString("<li>%1</li>").arg(it.current()->listenerInfoString);
00186     }
00187     openedObjectsStr += "</ul>";
00188     int r = KMessageBox::questionYesNo(parent, 
00189         "<p>"+msg+"</p><p>"+openedObjectsStr+"</p><p>"
00190         +i18n("Do you want to close all windows for these objects?"), 
00191         QString::null, KGuiItem(i18n("Close windows"),"fileclose"), KStdGuiItem::cancel());
00192     tristate res;
00193     if (r == KMessageBox::Yes) {
00194         //try to close every window
00195         res = conn.closeAllTableSchemaChangeListeners(table);
00196         if (res!=true) //do not expose closing errors twice; just cancel
00197             res = cancelled;
00198     }
00199     else
00200         res = cancelled;
00201 
00202     return res;
00203 }
00204 
00205 QString
00206 KexiTablePart::i18nMessage(const QCString& englishMessage, KexiDialogBase* dlg) const
00207 {
00208     if (englishMessage=="Design of object \"%1\" has been modified.")
00209         return i18n("Design of table \"%1\" has been modified.");
00210 
00211     if (englishMessage=="Object \"%1\" already exists.")
00212         return i18n("Table \"%1\" already exists.");
00213 
00214     if (dlg->currentViewMode()==Kexi::DesignViewMode && !dlg->neverSaved()
00215         && englishMessage==":additional message before saving design")
00216         return i18n("Warning! Any data in this table will be removed upon design's saving!");
00217 
00218     return englishMessage;
00219 }
00220 
00221 void KexiTablePart::setupCustomPropertyPanelTabs(KTabWidget *tab, KexiMainWindow* mainWin)
00222 {
00223     if (!d->lookupColumnPage) {
00224         d->lookupColumnPage = new KexiLookupColumnPage(0);
00225         connect(d->lookupColumnPage, SIGNAL(jumpToObjectRequested(const QCString&, const QCString&)),
00226             mainWin, SLOT(highlightObject(const QCString&, const QCString&)));
00227 
00229 
00230     /*
00231         connect(d->dataSourcePage, SIGNAL(formDataSourceChanged(const QCString&, const QCString&)),
00232             KFormDesigner::FormManager::self(), SLOT(setFormDataSource(const QCString&, const QCString&)));
00233         connect(d->dataSourcePage, SIGNAL(dataSourceFieldOrExpressionChanged(const QString&, const QString&, KexiDB::Field::Type)),
00234             KFormDesigner::FormManager::self(), SLOT(setDataSourceFieldOrExpression(const QString&, const QString&, KexiDB::Field::Type)));
00235         connect(d->dataSourcePage, SIGNAL(insertAutoFields(const QString&, const QString&, const QStringList&)),
00236             KFormDesigner::FormManager::self(), SLOT(insertAutoFields(const QString&, const QString&, const QStringList&)));*/
00237     }
00238 
00239     KexiProject *prj = mainWin->project();
00240     d->lookupColumnPage->setProject(prj);
00241 
00243     tab->addTab( d->lookupColumnPage, SmallIconSet("combo"), "");
00244     tab->setTabToolTip( d->lookupColumnPage, i18n("Lookup column"));
00245 }
00246 
00247 KexiLookupColumnPage* KexiTablePart::lookupColumnPage() const
00248 {
00249     return d->lookupColumnPage;
00250 }
00251 
00252 //----------------
00253 
00254 #if 0
00255 KexiTableDataSource::KexiTableDataSource(KexiPart::Part *part)
00256  : KexiPart::DataSource(part)
00257 {
00258 }
00259 
00260 KexiTableDataSource::~KexiTableDataSource()
00261 {
00262 }
00263 
00264 KexiDB::FieldList *
00265 KexiTableDataSource::fields(KexiProject *project, const KexiPart::Item &it)
00266 {
00267     kdDebug() << "KexiTableDataSource::fields(): " << it.name() << endl;
00268     return project->dbConnection()->tableSchema(it.name());
00269 }
00270 
00271 KexiDB::Cursor *
00272 KexiTableDataSource::cursor(KexiProject * /*project*/, 
00273     const KexiPart::Item &/*it*/, bool /*buffer*/)
00274 {
00275     return 0;
00276 }
00277 #endif
00278 
00279 //----------------
00280 
00281 KexiTablePart::TempData::TempData(QObject* parent)
00282  : KexiDialogTempData(parent)
00283  , table(0)
00284  , tableSchemaChangedInPreviousView(true /*to force reloading on startup*/ )
00285 {
00286 }
00287 
00288 //----------------
00289 
00293 /*
00294 AboutData( const char *programName,
00295     const char *version,
00296     const char *i18nShortDescription = 0,
00297     int licenseType = License_Unknown,
00298     const char *i18nCopyrightStatement = 0,
00299     const char *i18nText = 0,
00300     const char *homePageAddress = 0,
00301     const char *bugsEmailAddress = "submit@bugs.kde.org"
00302 );
00303 
00304 #define KEXIPART_EXPORT_FACTORY( libname, partClass, aboutData ) \
00305     static KexiPart::AboutData * libname ## updateAD(KexiPart::AboutData *ad) \
00306     { ad->setAppName( #libname ); return ad; } \
00307     K_EXPORT_COMPONENT_FACTORY( libname, KGenericFactory<partClass>(libname ## updateAD(#libname)) )
00308 */
00309 
00310 K_EXPORT_COMPONENT_FACTORY( kexihandler_table, KGenericFactory<KexiTablePart>("kexihandler_table") )
00311 
00312 #include "kexitablepart.moc"
00313 
KDE Home | KDE Accessibility Home | Description of Access Keys