From 0d72eff1d1ff0c4c69d465cef0ceb13d20f98836 Mon Sep 17 00:00:00 2001 From: Norbert Moutarde Date: Fri, 27 Dec 2013 17:16:58 +0100 Subject: [PATCH] I download file from the web ! (I have to parse/write data) --- insert-sql-android.sh | 14 +++++---- mainwindow.cpp | 73 ++++++++++++++++++++++++++++++------------- mainwindow.h | 40 ++++++++++++++++++++++++ ui.sql | 2 +- 4 files changed, 100 insertions(+), 29 deletions(-) diff --git a/insert-sql-android.sh b/insert-sql-android.sh index 82dd894..3c2e50a 100755 --- a/insert-sql-android.sh +++ b/insert-sql-android.sh @@ -1,11 +1,13 @@ -export DB=/data/data/eu.nothing2do.diary-mobile/files/db +export DB=/data/data/eu.nothing2do.diarymobile/files/db +export TMP=/storage/sdcard0 adb pull $DB echo "$DB file retreived" cat $1|sqlite3 db -echo "db file populated" -adb push db /storage/sdcard1 -echo "file db copied to /storage/sdcard1" -adb shell su -c mv /storage/sdcard1/db $DB -echo "file mv'ed to the right place" +echo "$1 is into db file" +adb push db $TMP +echo "file db copied to $TMP" +adb shell su -c cp $TMP/db $DB +adb shell su -c rm $TMP/db +echo "file cp'ed to the right place" adb shell su -c chown u0_a12:u0_a12 $DB echo "file chown'ed" diff --git a/mainwindow.cpp b/mainwindow.cpp index 44c6e8f..386f19f 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -136,20 +136,23 @@ void MainWindow::action(const QString &a){ flush(); if (a.split(QString(" "))[0]==QString("set")){ conf->setValue(QString(a.split(QString(" "))[1]), a.split(QString(" "))[2]); - start(); + getButtons(way.last()); } else if(a.split(QString(" "))[0]==QString("settext")){ conf->setValue(a.split(QString(" "))[1], QInputDialog::getText(this, "setText", a.split(QString(" "))[1], QLineEdit::Normal, conf->value(QString(a.split(QString(" "))[1])).toString()));//conf->value(QString(a.split(QString(" ")).mid(2))) + getButtons(way.last()); } else if(a.split(QString(" "))[0]==QString("setint")){ conf->setValue(a.split(QString(" "))[1], QInputDialog::getInt(this, "setInt", a.split(QString(" "))[2] , conf->value(QString(a.split(QString(" "))[3])).toInt())); + getButtons(way.last()); } else if(a.split(QString(" "))[0]=="new"){ entry(a.remove(0, 4)); - start(); + getButtons(way.last()); } else if(a.split(QString(" "))[0]=="setfile"){ conf->setValue(a.split(QString(" "))[1], QFileDialog::getSaveFileName(this, QString("get file"), QDir::homePath())); + getButtons(way.last()); } else getButtons(a); } @@ -254,6 +257,8 @@ void MainWindow::firstrun(){ conf->setValue(QString("menu"), QVariant(1)); conf->setValue(QString("sqlfile"), QVariant("ui.sql")); conf->setValue(QString("getsql"), QVariant("wget --no-check-certificate https://github.com/Nothing2Do/diary-mobile-android/raw/master/ui.sql")); + conf->setValue(QString("update"), QVariant("https://raw.github.com/Nothing2Do/diary-mobile-android/master/ui.sql")); + } QString &MainWindow::get(int row, int column){ q->seek(row); @@ -268,10 +273,10 @@ int MainWindow::exec(const QList & a){ } qDebug()<<"exec("<exec(); QSqlError b=q->lastError(); - int qs=q->size(); - //q->exec(QString("COMMIT;")); + if (b.isValid())qDebug()<<"SQL error->exec() :"<isSelect())return q->size(); + else return 0; } int MainWindow::alias(const QString & a){ qDebug()<<"alias("<prepare("INSERT INTO raw (date, texte) VALUES (:date, :txt);"); - q->bindValue(":date", time); - q->bindValue(":txt", entry); - qDebug()<<":date = "<boundValue(QString(":date")); - qDebug()<<":txt = "<boundValue(QString(":txt")); - q->exec(); + QList d({"INSERT INTO raw (date, texte) VALUES (:date, :txt);"}); + d.append(time); + d.append(entry); + exec(d); QSqlError b=q->lastError(); if (b.isValid())qDebug()<<"q->lastError():"< tmp({"select label,action from ui where keyword like :word;", a});; - QString b=QString::number(way.size()); - label->setText(b); + label->setText(QString::number(way.size())); way.append(a); exec(tmp); int labCol = 0;//r.indexOf("label"); @@ -364,17 +365,17 @@ void MainWindow::editButtonDB(){ }*/ } void MainWindow::updateUi(){ - // it work on linux - system(conf->value(QString("getsql")).toString().toStdString().c_str()); - QString cmd="cat "+conf->value(QString("dbfile")).toString()+"|sqlite3 "+conf->value(QString("database")).toString(); - qDebug()<<"cmd : "<value(QString("update")).toString(); + QUrl update(conf->value(QString("update")).toString()); + m = new FileDownloader(update, this); + connect(m, SIGNAL(downloaded()), SLOT(writeUpdate())); + + } void MainWindow::back(){ bool ok; - int a=QInputDialog::getInt(this, QString("back()"), QString("Ou ?"), way.size()-2, 0, way.size()-2, 1, &ok); + int a=QInputDialog::getInt(this, QString("back()"), QString("Quel page ?"), way.size()-2, 0, way.size()-2, 1, &ok); if(ok){ flush(); getButtons(way[a]); @@ -404,3 +405,31 @@ static void SetTextToLabel(QLabel *label, QString text) QString clippedText = metrix.elidedText(text, Qt::ElideRight, width); label->setText(clippedText); } +FileDownloader::FileDownloader(QUrl url, QObject *parent) : QObject(parent) +{ + connect(&m_WebCtrl, SIGNAL(finished(QNetworkReply*)), + SLOT(fileDownloaded(QNetworkReply*))); + + QNetworkRequest request(url); + m_WebCtrl.get(request); +} +FileDownloader::~FileDownloader() +{ + +} +void FileDownloader::fileDownloaded(QNetworkReply* pReply) +{ + m_DownloadedData = pReply->readAll(); + //emit a signal + pReply->deleteLater(); + emit downloaded(); +} +QByteArray FileDownloader::downloadedData() const +{ + return m_DownloadedData; +} +void MainWindow::writeUpdate(){ + qDebug()<<"downloaded :"<downloadedData(); + /* put newly downloaded sql file into database */ + +} diff --git a/mainwindow.h b/mainwindow.h index 14de854..91a0da2 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -20,6 +20,43 @@ //#include #include +#ifndef FILEDOWNLOADER_H +#define FILEDOWNLOADER_H + +#include +#include +#include +#include +#include + +class FileDownloader : public QObject +{ + Q_OBJECT +public: + explicit FileDownloader(QUrl url, QObject *parent = 0); + + virtual ~FileDownloader(); + + QByteArray downloadedData() const; + +signals: + void downloaded(); + +private slots: + + void fileDownloaded(QNetworkReply* pReply); + +private: + + QNetworkAccessManager m_WebCtrl; + + QByteArray m_DownloadedData; + +}; + +#endif // FILEDOWNLOADER_H + + class CLabel : public QLabel { Q_OBJECT @@ -38,6 +75,7 @@ protected: void mousePressEvent ( QMouseEvent * event ) ; void mouseReleaseEvent(QMouseEvent * event ) ; }; + class MainWindow : public QMainWindow { Q_OBJECT @@ -82,6 +120,7 @@ private slots: void getButtons(const QString &); //void getButtons(); void updateUi(); + void writeUpdate(); public slots: signals: @@ -101,6 +140,7 @@ private: QSqlDatabase db; QSqlQuery *q; //QSqlRecord r; + FileDownloader * m; }; diff --git a/ui.sql b/ui.sql index fc44ef9..7092444 100644 --- a/ui.sql +++ b/ui.sql @@ -28,6 +28,6 @@ INSERT INTO "ui" (keyword, label, action) VALUES('config','database','settext db INSERT INTO "ui" (keyword, label, action) VALUES('config','filesave','settext file'); INSERT INTO "ui" (keyword, label, action) VALUES('config','SQLsettings', 'settext SQLsettings requiressl=1 connect_timeout=10000'); INSERT INTO "ui" (keyword, label, action) VALUES('config','button', 'settext button background-color: red;border-style: outset;border-width: 7px;border-radius: 10px;border-color: beige;font: bold 15px;padding: 6px;'); -INSERT INTO "ui" (keyword, label, action) VALUES('config','fichier SQL', 'setfile sqlfile ui.sql'); +INSERT INTO "ui" (keyword, label, action) VALUES('config','fichier SQL', 'settext sqlfile ui.sql'); INSERT INTO "ui" (keyword, label, action) VALUES('config','getSQL', 'settext getsql wget --no-check-certificate https://github.com/Nothing2Do/diary-mobile-android/raw/master/ui.sql'); COMMIT; -- 2.45.1