summaryrefslogtreecommitdiffstats
path: root/src/convert_sqlite3.cpp
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2024-10-13 11:56:14 +0900
committerMichele Calgaro <michele.calgaro@yahoo.it>2024-10-21 09:29:11 +0900
commit0c8ed6c9a4000af8f48581a81c4b5c2f5b9fd502 (patch)
tree10f9d3223f0a0904a0748a28ca44da52ee1092b7 /src/convert_sqlite3.cpp
parent7d5ba3180a82a0827c1fbd6dc93a2abf4f882c37 (diff)
downloadkrecipes-0c8ed6c9a4000af8f48581a81c4b5c2f5b9fd502.tar.gz
krecipes-0c8ed6c9a4000af8f48581a81c4b5c2f5b9fd502.zip
Rearrange folders structure to remove unnecessary 'krecipes' second level subfolder
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'src/convert_sqlite3.cpp')
-rw-r--r--src/convert_sqlite3.cpp121
1 files changed, 121 insertions, 0 deletions
diff --git a/src/convert_sqlite3.cpp b/src/convert_sqlite3.cpp
new file mode 100644
index 0000000..ddb144e
--- /dev/null
+++ b/src/convert_sqlite3.cpp
@@ -0,0 +1,121 @@
+/***************************************************************************
+* Copyright (C) 2005 by *
+* Jason Kivlighn (jkivlighn@gmail.com *
+* *
+* This program is free software; you can redistribute it and/or modify *
+* it under the terms of the GNU General Public License as published by *
+* the Free Software Foundation; either version 2 of the License, or *
+* (at your option) any later version. *
+***************************************************************************/
+
+#include "convert_sqlite3.h"
+
+#include <tqapplication.h>
+#include <tqfile.h>
+
+#include <kdebug.h>
+#include <tdeglobal.h>
+#include <tdeconfig.h>
+#include <tdemessagebox.h>
+#include <kprocio.h>
+
+//FIXME: Some messages should be given to the user about success/failure, but that can't be done in the 0.8.x branch due to i18n.
+
+ConvertSQLite3::ConvertSQLite3( const TQString &db_file ) : TQObject(), error(false)
+{
+ TQString file = db_file;
+ if ( file.isEmpty() ) {
+ TDEConfig *config = TDEGlobal::config();
+ config->setGroup("Server");
+ file = config->readEntry("DBFile");
+ }
+
+ KProcIO *p = new KProcIO;
+ p->setUseShell(true);
+
+ //sqlite OLD.DB .dump | sqlite3 NEW.DB
+ *p << "sqlite" << file << ".dump" <<
+ "|" << "sqlite3" << file+".new";
+
+ TQApplication::connect( p, TQ_SIGNAL(readReady(KProcIO*)), this, TQ_SLOT(processOutput(KProcIO*)) );
+
+ bool success = p->start( TDEProcess::Block, true );
+ if ( !success ) {
+ kdDebug()<<"Conversion failed... unable to start TDEProcess"<<endl;
+ return;
+ }
+
+ if ( error )
+ return;
+
+
+ TQString backup_file = file+".sqlite2";
+ int i = 1;
+ while ( TQFile::exists(backup_file) ) {
+ backup_file = backup_file.left(file.length()+8)+"."+TQString::number(i);
+ ++i;
+ }
+
+ if ( !copyFile( file, backup_file ) ) {
+ kdDebug()<<"Unable to backup SQLite 2 database... aborting"<<endl
+ <<"A successfully converted SQLite 3 file is available at \""<<file<<".new\"."<<endl;
+ }
+ else {
+ kdDebug()<<"SQLite 2 database backed up to "<<backup_file<<endl;
+ if ( !copyFile( file+".new", file ) ) {
+ kdDebug()<<"Unable to copy the new SQLite 3 database to: "<<file<<"."<<endl
+ <<"You may manually move \""<<file<<".new\" to \""<<file<<"\""<<endl;
+ }
+ else {
+ kdDebug()<<"Conversion successful!"<<endl;
+ TQFile::remove(file+".new");
+ }
+ }
+}
+
+ConvertSQLite3::~ConvertSQLite3()
+{
+}
+
+void ConvertSQLite3::processOutput( KProcIO* p )
+{
+ TQString error_str, buffer;
+ while ( p->readln(buffer) != -1 ) {
+ error_str += buffer;
+ }
+
+ KMessageBox::error( 0, error_str );
+
+ error = true;
+
+ p->ackRead();
+}
+
+bool ConvertSQLite3::copyFile( const TQString &oldFilePath, const TQString &newFilePath )
+{
+ //load both files
+ TQFile oldFile(oldFilePath);
+ TQFile newFile(newFilePath);
+ bool openOld = oldFile.open( IO_ReadOnly );
+ bool openNew = newFile.open( IO_WriteOnly );
+
+ //if either file fails to open bail
+ if(!openOld || !openNew) { return false; }
+
+ //copy contents
+ uint BUFFER_SIZE = 16000;
+ char* buffer = new char[BUFFER_SIZE];
+ while(!oldFile.atEnd())
+ {
+ TQ_ULONG len = oldFile.readBlock( buffer, BUFFER_SIZE );
+ newFile.writeBlock( buffer, len );
+ }
+
+ //deallocate buffer
+ delete[] buffer;
+ buffer = NULL;
+ return true;
+}
+
+#include "convert_sqlite3.moc"
+