简体   繁体   中英

Determine Google Drive location with Qt/C++

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 ?

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM