~ubuntu-branches/ubuntu/vivid/unity-webapps-qml/vivid

« back to all changes in this revision

Viewing changes to src/Ubuntu/UnityWebApps/plugin/unity-webapps-app-model.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Ubuntu daily release, Alexandre Abreu
  • Date: 2014-05-14 22:31:22 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20140514223122-ln3ei1hz5je1tkc6
Tags: 0.1+14.10.20140514.2-0ubuntu1
[ Ubuntu daily release ]
* New rebuild forced

[ Alexandre Abreu ]
* Cleanup the structure of the QML bindings to make it clearer. No
  Changes in functionality all features are still working & same, This
  is an updated version of: https://code.launchpad.net/~abreu-
  alexandre/unity-webapps-qml/restructure-cleanup-bindings-qml-
  structure/+merge/208148 with the latest changes that got in in
  between (LP: #1288801)
* Simplifies the manifest.json handling in some instances. More
  precisely when one is using a custom model search path for the
  webapp lookup, some restrictions are being put on what is expected
  to be found in the folder among which: a common/ subfolder (even if
  the webapp doesnt require one),. one or more unity-webapps-* folders
  with manifest.json & *.user.js files in it,. More over the manifest
  files do require user scripts to be defined. This imposes a set of
  constraints over a given webapp that simply wants to use the
  container (w/o js injection). This simplifies it an allows one to
  define a manifest.json file (still with some required elements, but
  a smaller set), directly in the model search path. E.g. something
  like: {     "name": "MyWebApp",     "homepage":
  "http://www.bbc.co.uk/news/",     "domain": "bbc.co.uk",
      "includes": [] } will do, or to avoid any confusion w/ the click
  manifest.json file, one also searches for a webapp-properties.json
  w/ the same content as above.

Show diffs side-by-side

added added

removed removed

Lines of Context:
161
161
}
162
162
 
163
163
UnityWebappsAppModel::WebappFileInfoOption
164
 
UnityWebappsAppModel::getWebappFiles(QFileInfo webAppInstallLocation)
 
164
UnityWebappsAppModel::getWebappFiles(const QFileInfo& webAppInstallLocation)
165
165
{
166
166
    if (!webAppInstallLocation.isDir()) {
167
167
        qDebug() << "Invalid webapps path found (not a proper folder): "
171
171
 
172
172
    QDir installationDir = QDir(webAppInstallLocation.absoluteFilePath());
173
173
 
174
 
    //TODO search only for manifest.json
175
 
    // Search for manifest & userscript
176
 
    QFileInfoList manifest =
177
 
            installationDir.entryInfoList(QStringList("*.json"), QDir::Files);
178
 
    if (manifest.count() != 1) {
179
 
        qDebug() << "Folder not being considered for webapp (no manifest file found): "
180
 
                 << installationDir.absolutePath();
181
 
        return WebappFileInfoOption();
182
 
    }
183
 
 
184
 
    QFileInfoList script =
185
 
            installationDir.entryInfoList(QStringList("*.user.js"), QDir::Files);
186
 
    if (script.count() != 1) {
187
 
        qDebug() << "Folder not being considered for webapp (no userscript found): "
188
 
                 << installationDir.absolutePath();
189
 
        return WebappFileInfoOption();
190
 
    }
191
 
 
192
 
    return WebappFileInfoOption (WebappFileInfo (manifest[0].absoluteFilePath(), script[0].absoluteFilePath()));
 
174
    // Search for manifest files & userscript
 
175
 
 
176
    // The order here is important, the preferred filename is manifest.json
 
177
    // which is still used in the desktop for regulat webapps, but in order
 
178
    // to avoid name clashes w/ the click package manifest.json, the
 
179
    // webapp-properties.json filename is searches first as a valid name
 
180
    // and the regular manifest.json file used as a standard fallback.
 
181
    QStringList manifestFileNames =
 
182
            QStringList() << QString("webapp-properties.json")
 
183
                          << QString("manifest.json");
 
184
 
 
185
    WebappFileInfoOption
 
186
            webappCandidateInfo;
 
187
    Q_FOREACH(QString manifestFileName, manifestFileNames)
 
188
    {
 
189
        QFileInfo manifestFileInfo =
 
190
                installationDir.absolutePath() + QDir::separator() + manifestFileName;
 
191
        if ( ! manifestFileInfo.isFile()) {
 
192
            qDebug() << "Skipping" << manifestFileName << "as a webapp definition search: "
 
193
                     << manifestFileInfo.absoluteFilePath();
 
194
            continue;
 
195
        }
 
196
 
 
197
        QString userScriptFilename;
 
198
        QFileInfoList script =
 
199
                installationDir.entryInfoList(QStringList("*.user.js"), QDir::Files);
 
200
        if (script.count() >= 1) {
 
201
            // Arbitrarily considering the "first" one
 
202
            userScriptFilename = script[0].absoluteFilePath();
 
203
        }
 
204
 
 
205
        webappCandidateInfo = WebappFileInfoOption (WebappFileInfo (manifestFileInfo.absoluteFilePath(),
 
206
                                                                    userScriptFilename));
 
207
 
 
208
        break;
 
209
    }
 
210
 
 
211
    return webappCandidateInfo;
193
212
}
194
213
 
195
214
QFileInfoList
196
215
UnityWebappsAppModel::getCandidateInstalledWebappsFolders (const QString& installationSearchPath)
197
216
{
 
217
    // If the search path was overriden (not the default system one), we add the local
 
218
    // path to the list of searched paths. The idea is to remove some of the potentially
 
219
    // overloaded cruft (notably on UbuntuTouch or for simple installs/tests) the needed
 
220
    // webapp file setup.
 
221
    // We still do some possibly unecessary work of looking up in any unity-webapp-* subdir
 
222
    // but in the general case that shouldn't be too much of a hassle.
 
223
 
198
224
    QDir webappsDir(installationSearchPath);
199
 
    return webappsDir.entryInfoList (QStringList(_webappDirPrefix + "*"), QDir::Dirs);
 
225
    QFileInfoList
 
226
        candidateWebappsFolders = webappsDir.entryInfoList (QStringList(_webappDirPrefix + "*")
 
227
                                                            , QDir::Dirs);
 
228
    if (installationSearchPath != getDefaultWebappsInstallationSearchPath())
 
229
    {
 
230
        QFileInfo localSearchPath(installationSearchPath);
 
231
        if (localSearchPath.isDir())
 
232
        {
 
233
            candidateWebappsFolders.append(localSearchPath);
 
234
        }
 
235
    }
 
236
    return candidateWebappsFolders;
200
237
}
201
238
 
202
239
void UnityWebappsAppModel::cleanup()
223
260
    {
224
261
        if (!candidateWebappFolder.isDir())
225
262
        {
226
 
            qDebug() << "Candidate webapp folder not a webapp: "
227
 
                     << candidateWebappFolder.absoluteFilePath();
228
263
            continue;
229
264
        }
230
265
 
232
267
                getWebappFiles (candidateWebappFolder);
233
268
        if (!webappInfos.isvalid())
234
269
        {
235
 
            qDebug() << "Invalid webapps path found: "
236
 
                     << candidateWebappFolder.absoluteFilePath();
237
270
            continue;
238
271
        }
239
272
 
244
277
                parser.parse (QFileInfo (webappInfos.value().manifestFilename));
245
278
        if (!manifest.isvalid())
246
279
        {
247
 
            qDebug() << "Invalid webapps installation found: "
 
280
            qDebug() << "Invalid webapps manifest found in: "
248
281
                     << candidateWebappFolder.absoluteFilePath();
249
282
            continue;
250
283
        }
430
463
bool UnityWebappsAppModel::isValidInstall(const QString& searchPath)
431
464
{
432
465
    return QFileInfo(searchPath).isDir()
433
 
           && QDir(searchPath).exists()
434
 
           && QFileInfo(searchPath + QDir::separator() + _commonScriptsDirName).isDir()
435
 
           && QDir(searchPath + QDir::separator() + _commonScriptsDirName).exists();
 
466
           && QDir(searchPath).exists();
436
467
}
437
468
 
438
469
int UnityWebappsAppModel::rowCount(const QModelIndex& parent) const