| 922 | } |
| 923 | |
| 924 | void Backend::fetchGoogleDriveApps(const QString &token) |
| 925 | { |
| 926 | // Step 1: Find "CloudRedirect" folder in Drive root |
| 927 | QString query = QString("name='CloudRedirect' and mimeType='application/vnd.google-apps.folder' and trashed=false"); |
| 928 | QUrl url("https://www.googleapis.com/drive/v3/files"); |
| 929 | QUrlQuery params; |
| 930 | params.addQueryItem("q", query); |
| 931 | params.addQueryItem("fields", "files(id)"); |
| 932 | params.addQueryItem("spaces", "drive"); |
| 933 | url.setQuery(params); |
| 934 | |
| 935 | QNetworkRequest req(url); |
| 936 | req.setRawHeader("Authorization", ("Bearer " + token).toUtf8()); |
| 937 | |
| 938 | auto *reply = m_nam->get(req); |
| 939 | connect(reply, &QNetworkReply::finished, this, [this, reply, token]() { |
| 940 | reply->deleteLater(); |
| 941 | if (reply->error() != QNetworkReply::NoError) { |
| 942 | fprintf(stderr, "[Backend] GDrive: failed to find root folder: %s\n", |
| 943 | reply->errorString().toUtf8().constData()); |
| 944 | emit remoteAppsFetched(); |
| 945 | return; |
| 946 | } |
| 947 | |
| 948 | QJsonDocument doc = QJsonDocument::fromJson(reply->readAll()); |
| 949 | QJsonArray files = doc.object().value("files").toArray(); |
| 950 | if (files.isEmpty()) { |
| 951 | fprintf(stderr, "[Backend] GDrive: CloudRedirect folder not found\n"); |
| 952 | emit remoteAppsFetched(); |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | QString rootFolderId = files[0].toObject().value("id").toString(); |
| 957 | |
| 958 | // Step 2: Find account folder inside CloudRedirect |
| 959 | QString query2 = QString("name='%1' and '%2' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false") |
| 960 | .arg(m_accountId, rootFolderId); |
| 961 | QUrl url2("https://www.googleapis.com/drive/v3/files"); |
| 962 | QUrlQuery params2; |
| 963 | params2.addQueryItem("q", query2); |
| 964 | params2.addQueryItem("fields", "files(id)"); |
| 965 | url2.setQuery(params2); |
| 966 | |
| 967 | QNetworkRequest req2(url2); |
| 968 | req2.setRawHeader("Authorization", ("Bearer " + token).toUtf8()); |
| 969 | |
| 970 | auto *reply2 = m_nam->get(req2); |
| 971 | connect(reply2, &QNetworkReply::finished, this, [this, reply2, token, rootFolderId]() { |
| 972 | reply2->deleteLater(); |
| 973 | if (reply2->error() != QNetworkReply::NoError) { |
| 974 | fprintf(stderr, "[Backend] GDrive: failed to find account folder\n"); |
| 975 | emit remoteAppsFetched(); |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | QJsonDocument doc2 = QJsonDocument::fromJson(reply2->readAll()); |
| 980 | QJsonArray files2 = doc2.object().value("files").toArray(); |
| 981 | if (files2.isEmpty()) { |