Lomiri
WallpaperResolver.qml
1 /*
2  * Copyright (C) 2015 Canonical Ltd.
3  * Copyright (C) 2021 UBports Foundation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 import QtQuick 2.4
19 import Lomiri.Components 1.3
20 
21 Item {
22  id: root
23 
24  // Provide a list of wallpapers to resolve here, preferred ones first
25  property var candidates: []
26 
27  property bool cache: true
28 
29  readonly property url background: {
30  for (var i = 0; i < repeater.count; i++) {
31  var image = repeater.itemAt(i);
32  var expectedImageSource = Qt.resolvedUrl(candidates[i]);
33  if (image.source != expectedImageSource)
34  return "";
35  if (image.status === Image.Ready)
36  return candidates[i];
37  if (image.status === Image.Loading)
38  return "";
39  }
40  if (candidates.length > 0) {
41  return candidates.slice(-1)[0]; // last item is last resort
42  } else {
43  return "";
44  }
45  }
46 
47  Repeater {
48  id: repeater
49  model: root.candidates.slice(0, -1)
50  delegate: Image {
51  source: modelData
52  asynchronous: true
53  cache: root.cache
54  height: 0
55  width: 0
56  sourceSize.height: 1
57  sourceSize.width: 1
58  }
59  }
60 }