00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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
00056 }
00057
00058 KexiTablePart::~KexiTablePart()
00059 {
00060 }
00061
00062 void KexiTablePart::initPartActions()
00063 {
00064 }
00065
00066 void KexiTablePart::initInstanceActions()
00067 {
00068
00069
00070 KAction *a = createSharedToggleAction(
00071 Kexi::DesignViewMode, i18n("Primary Key"), "key", 0, "tablepart_toggle_pkey");
00072
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;
00102
00103
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
00126 return conn->removeObject( item.identifier() );
00127 }
00128
00129 tristate KexiTablePart::rename(KexiMainWindow *win, KexiPart::Item & item,
00130 const QString& newName)
00131 {
00132
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
00177 res = conn.closeAllTableSchemaChangeListeners(table);
00178 if (res!=true)
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 * ,
00224 const KexiPart::Item &, bool )
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 )
00236 {
00237 }
00238
00239
00240
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261 K_EXPORT_COMPONENT_FACTORY( kexihandler_table, KGenericFactory<KexiTablePart>("kexihandler_table") )
00262
00263 #include "kexitablepart.moc"
00264