2

Essentially this is a Qt/C++ variant of the question here :

How do I programmatically locate my Google Drive folder using C#?

What I have tried so far is given below

    QString gDrivePath =  QDesktopServices::storageLocation(QDesktopServices::DataLocation);
    gDrivePath += "\\Google\\Drive\\sync_config.db";
    bool result = QFile::copy(gDrivePath, "gdrive.db");

    QFile gdrivefile("gdrive.db");
    if(!gdrivefile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::information(0, "Google Drive path read error", gdrivefile.errorString());
    }

    QTextStream gin(&gdrivefile);
    gin.setCodec("ASCII");
    while(!gin.atEnd()) {
        qDebug() << gin.readLine().toLocal8Bit();
    }
    gdrivefile.close();

Like given in the question linked to above, the code makes a copy of the db file and tries to read it line by line. The only problem being that it is skipping a chunk of data from the file which contains the required "local_sync_root_pathvalue"

Any ideas ?

Community
  • 1
  • 1
Prad Lal
  • 113
  • 7

1 Answers1

1

I ended up using sql classes from qt to achieve the feat.

Code:

    QString gDrivePath =  QDesktopServices::storageLocation(QDesktopServices::DataLocation);
    gDrivePath += "\\Google\\Drive\\sync_config.db";
    QFile::copy(gDrivePath, "gdrive.db");

    QFile gDriveFile("gdrive.db");
    if(!gDriveFile.open(QIODevice::ReadOnly)) {
        qDebug() << "Error in opening Google Drive File" << gDriveFile.errorString();
    }
    else {
        QSqlDatabase gDrivedb = QSqlDatabase::addDatabase("QSQLITE");
        gDrivedb.setDatabaseName("gdrive.db");
        if (gDrivedb.open()) {
            QSqlQuery query(gDrivedb);
            if (query.exec("select * from data where entry_key='local_sync_root_path'")) {
                 while (query.next()) {
                     QString gDrivePath = query.value(2).toString().remove(0,4);
                     qDebug() << "Google Drive at - " << gDrivePath;
                 }
            } else {
                qDebug() << "Error in querying Google Drive db" << query.lastError();
            }
        }
        else
            qDebug() << "Error in Opening  Google Drive db";
        gDriveFile.close();
    }
Prad Lal
  • 113
  • 7
  • 1
    Don't make a copy of the file, it's not only not necessary, but it can result in a corrupted copy if google drive happens to be modifying the file while you make a copy. – Kuba hasn't forgotten Monica Sep 06 '13 at 08:17
  • Well, you're right. That copy operation is unnecessary and indeterministic. Also followed from the deleted answer. I have tested the code without the copy and it works fine. Thanks @KubaOber – Prad Lal Sep 07 '13 at 17:57
  • the first line does not work : `storageLocation' is not a member of 'qdesktopservices'` – McLan Mar 29 '16 at 15:17