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 
00028 #include "keximainwindow.h"
00029 #include "kexiproject.h"
00030 #include "kexipartinfo.h"
00031 #include "widget/kexidatatable.h"
00032 #include "widget/kexidatatableview.h"
00033 #include "kexialtertabledialog.h"
00034 #include "kexialtertable_dataview.h"
00035 
00036 #include <kexidb/connection.h>
00037 #include <kexidb/cursor.h>
00038 #include <kexidialogbase.h>
00039 
00040 
00041 KexiTablePart::KexiTablePart(QObject *parent, const char *name, const QStringList &l)
00042  : KexiPart::Part(parent, name, l)
00043 {
00044     // REGISTERED ID:
00045     m_registeredPartID = (int)KexiPart::TableObjectType;
00046 
00047     kdDebug() << "KexiTablePart::KexiTablePart()" << endl;
00048     m_names["instanceName"] 
00049         = i18n("Translate this word using only lowercase alphanumeric characters (a..z, 0..9). "
00050         "Use '_' character instead of spaces. First character should be a..z character. "
00051         "If you cannot use latin characters in your language, use english word.", 
00052         "table");
00053     m_names["instanceCaption"] = i18n("Table");
00054     m_supportedViewModes = Kexi::DataViewMode | Kexi::DesignViewMode;
00055 //js TODO: also add Kexi::TextViewMode when we'll have SQL ALTER TABLE EDITOR!!!
00056 }
00057 
00058 KexiTablePart::~KexiTablePart()
00059 {
00060 }
00061 
00062 void KexiTablePart::initPartActions()
00063 {
00064 }
00065 
00066 void KexiTablePart::initInstanceActions()
00067 {
00068 //moved to main window  createSharedAction(Kexi::DataViewMode, i18n("Filter"), "filter", 0, "tablepart_filter");
00069 
00070     KAction *a = createSharedToggleAction(
00071         Kexi::DesignViewMode, i18n("Primary Key"), "key", 0, "tablepart_toggle_pkey");
00072 //      Kexi::DesignViewMode, i18n("Toggle Primary Key"), "key", 0, "tablepart_toggle_pkey");
00073     a->setWhatsThis(i18n("Sets or removes primary key for currently selected field."));
00074 }
00075 
00076 KexiDialogTempData* KexiTablePart::createTempData(KexiDialogBase* dialog)
00077 {
00078     return new KexiTablePart::TempData(dialog);
00079 }
00080 
00081 KexiViewBase* KexiTablePart::createView(QWidget *parent, KexiDialogBase* dialog, 
00082     KexiPart::Item &item, int viewMode, QMap<QString,QString>*)
00083 {
00084     KexiMainWindow *win = dialog->mainWin();
00085     if (!win || !win->project() || !win->project()->dbConnection())
00086         return 0;
00087 
00088 
00089     KexiTablePart::TempData *temp = static_cast<KexiTablePart::TempData*>(dialog->tempData());
00090     if (!temp->table) {
00091         temp->table = win->project()->dbConnection()->tableSchema(item.name());
00092         kdDebug() << "KexiTablePart::execute(): schema is " << temp->table << endl;
00093     }
00094 
00095     if (viewMode == Kexi::DesignViewMode) {
00096         KexiAlterTableDialog *t = new KexiAlterTableDialog(win, parent, "altertable");
00097         return t;
00098     }
00099     else if (viewMode == Kexi::DataViewMode) {
00100         if(!temp->table)
00101             return 0; //todo: message
00102         //we're not setting table schema here -it will be forced to set 
00103         // in KexiAlterTable_DataView::afterSwitchFrom()
00104         KexiAlterTable_DataView *t = new KexiAlterTable_DataView(win, parent, "dataview");
00105         return t;
00106     }
00107     return 0;
00108 }
00109 
00110 bool KexiTablePart::remove(KexiMainWindow *win, KexiPart::Item &item)
00111 {
00112     if (!win || !win->project() || !win->project()->dbConnection())
00113         return false;
00114 
00115     KexiDB::Connection *conn = win->project()->dbConnection();
00116     KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());
00117 
00118     if (sch) {
00119         tristate res = KexiTablePart::askForClosingObjectsUsingTableSchema(
00120             win, *conn, *sch, 
00121             i18n("You are about to remove table \"%1\" but following objects using this table are opened:")
00122             .arg(sch->name()));
00123         return conn->dropTable( sch );
00124     }
00125     //last chance: just remove item
00126     return conn->removeObject( item.identifier() );
00127 }
00128 
00129 tristate KexiTablePart::rename(KexiMainWindow *win, KexiPart::Item & item, 
00130     const QString& newName)
00131 {
00132 //TODO: what about objects (queries/forms) that use old name?
00133     KexiDB::Connection *conn = win->project()->dbConnection();
00134     KexiDB::TableSchema *sch = conn->tableSchema(item.identifier());
00135     if (!sch)
00136         return false;
00137     return conn->alterTableName(*sch, newName);
00138 }
00139 
00140 KexiDB::SchemaData*
00141 KexiTablePart::loadSchemaData(KexiDialogBase *dlg, const KexiDB::SchemaData& sdata, int viewMode)
00142 {
00143     Q_UNUSED( viewMode );
00144 
00145     return dlg->mainWin()->project()->dbConnection()->tableSchema( sdata.name() );
00146 }
00147 
00148 #if 0
00149 KexiPart::DataSource *
00150 KexiTablePart::dataSource()
00151 {
00152     return new KexiTableDataSource(this);
00153 }
00154 #endif
00155 
00156 tristate KexiTablePart::askForClosingObjectsUsingTableSchema(QWidget *parent, KexiDB::Connection& conn, 
00157     KexiDB::TableSchema& table, const QString& msg)
00158 {
00159     QPtrList<KexiDB::Connection::TableSchemaChangeListenerInterface>* listeners
00160         = conn.tableSchemaChangeListeners(table);
00161     if (!listeners || listeners->isEmpty())
00162         return true;
00163     
00164     QString openedObjectsStr = "<ul>";
00165     for (QPtrListIterator<KexiDB::Connection::TableSchemaChangeListenerInterface> it(*listeners);
00166         it.current(); ++it) {
00167             openedObjectsStr += QString("<li>%1</li>").arg(it.current()->listenerInfoString);
00168     }
00169     openedObjectsStr += "</ul>";
00170     int r = KMessageBox::questionYesNo(parent, 
00171         "<p>"+msg+"</p><p>"+openedObjectsStr+"</p><p>"
00172         +i18n("Do you want to close all windows for these objects?"), 
00173         QString::null, KGuiItem(i18n("Close windows"),"fileclose"), KStdGuiItem::cancel());
00174     tristate res;
00175     if (r == KMessageBox::Yes) {
00176         //try to close every window
00177         res = conn.closeAllTableSchemaChangeListeners(table);
00178         if (res!=true) //do not expose closing errors twice; just cancel
00179             res = cancelled;
00180     }
00181     else
00182         res = cancelled;
00183 
00184     return res;
00185 }
00186 
00187 QString
00188 KexiTablePart::i18nMessage(const QCString& englishMessage, KexiDialogBase* dlg) const
00189 {
00190     if (englishMessage=="Design of object \"%1\" has been modified.")
00191         return i18n("Design of table \"%1\" has been modified.");
00192 
00193     if (englishMessage=="Object \"%1\" already exists.")
00194         return i18n("Table \"%1\" already exists.");
00195 
00196     if (dlg->currentViewMode()==Kexi::DesignViewMode && !dlg->neverSaved()
00197         && englishMessage==":additional message before saving design")
00198         return i18n("Note: Any data in this table will be removed upon design's saving.");
00199 
00200     return englishMessage;
00201 }
00202 
00203 //----------------
00204 
00205 #if 0
00206 KexiTableDataSource::KexiTableDataSource(KexiPart::Part *part)
00207  : KexiPart::DataSource(part)
00208 {
00209 }
00210 
00211 KexiTableDataSource::~KexiTableDataSource()
00212 {
00213 }
00214 
00215 KexiDB::FieldList *
00216 KexiTableDataSource::fields(KexiProject *project, const KexiPart::Item &it)
00217 {
00218     kdDebug() << "KexiTableDataSource::fields(): " << it.name() << endl;
00219     return project->dbConnection()->tableSchema(it.name());
00220 }
00221 
00222 KexiDB::Cursor *
00223 KexiTableDataSource::cursor(KexiProject * /*project*/, 
00224     const KexiPart::Item &/*it*/, bool /*buffer*/)
00225 {
00226     return 0;
00227 }
00228 #endif
00229 
00230 //----------------
00231 
00232 KexiTablePart::TempData::TempData(QObject* parent)
00233  : KexiDialogTempData(parent)
00234  , table(0)
00235  , tableSchemaChangedInPreviousView(true /*to force reloading on startup*/ )
00236 {
00237 }
00238 
00239 //----------------
00240 
00244 /*
00245 AboutData( const char *programName,
00246     const char *version,
00247     const char *i18nShortDescription = 0,
00248     int licenseType = License_Unknown,
00249     const char *i18nCopyrightStatement = 0,
00250     const char *i18nText = 0,
00251     const char *homePageAddress = 0,
00252     const char *bugsEmailAddress = "submit@bugs.kde.org"
00253 );
00254 
00255 #define KEXIPART_EXPORT_FACTORY( libname, partClass, aboutData ) \
00256     static KexiPart::AboutData * libname ## updateAD(KexiPart::AboutData *ad) \
00257     { ad->setAppName( #libname ); return ad; } \
00258     K_EXPORT_COMPONENT_FACTORY( libname, KGenericFactory<partClass>(libname ## updateAD(#libname)) )
00259 */
00260 
00261 K_EXPORT_COMPONENT_FACTORY( kexihandler_table, KGenericFactory<KexiTablePart>("kexihandler_table") )
00262 
00263 #include "kexitablepart.moc"
00264 
KDE Home | KDE Accessibility Home | Description of Access Keys