Debian Codegraph @

src/GUI/qml/AddCatalogPanel.qml

raw
1 import QtQuick 2.4
2 import FlightGear.Launcher 1.0 as FG
3 import FlightGear 1.0
4
5 Rectangle {
6 id: addCatalogPanel
7 state: "start"
8 height: column.height
9 color: Style.panelBackground
10
11 readonly property bool isActive: (state != "start")
12 Behavior on height {
13 enabled: false // disable to prevent animation on load
14 id: heightBehaviour
15 NumberAnimation { duration: 200; }
16 }
17
18 Timer { // ugly: timer to enable the animation after loading is done
19 onTriggered: heightBehaviour.enabled = true;
20 running: true
21 interval: 5
22 }
23
24 Column {
25 id: column
26 width: parent.width
27 spacing: Style.margin
28
29 ClickableText {
30 id: addCatalogPromptText
31
32 width: parent.width
33 wrapMode: Text.WordWrap
34
35 onClicked: {
36 addCatalogPanel.state = "enter-url";
37 }
38 }
39
40 LineEdit {
41 id: addCatalogUrlEntry
42 width: parent.width
43 useFullWidth: true
44 label: qsTr("Hangar URL:");
45 placeholder: "http://www.flightgear.org/hangars/some-catalog.xml"
46 }
47
48 AnimatedImage {
49 id: spinner
50 anchors.horizontalCenter: parent.horizontalCenter
51 source: "qrc:///linear-spinner"
52 playing: true
53 }
54
55 Item {
56 id: buttonBox
57 width: parent.width
58 height: childrenRect.height
59
60 Button {
61 id: addCatalogCancelButton
62 anchors.right: addCatalogProceedButton.left
63 anchors.rightMargin: Style.margin
64
65 text: qsTr("Cancel")
66
67 onClicked: {
68 _addOns.catalogs.abandonAddCatalog();
69 addCatalogPanel.state = "start";
70 }
71 }
72
73 Button {
74 id: addCatalogProceedButton
75 anchors.right: parent.right
76
77 text: qsTr("Add hangar")
78
79 // todo check for a well-formed URL
80 enabled: addCatalogUrlEntry.text !== ""
81
82 onClicked: {
83 // this occurs in the retry case; abandon the current
84 // add before starting the new one
85 if (_addOns.catalogs.isAddingCatalog) {
86 _addOns.catalogs.abandonAddCatalog();
87 }
88
89 _addOns.catalogs.addCatalogByUrl(addCatalogUrlEntry.text)
90 addCatalogPanel.state = "add-in-progress"
91 }
92 }
93 } // of button box
94 } // of column layout
95
96 Connections {
97 target: _addOns.catalogs
98 function onStatusOfAddingCatalogChanged() {
99 var status = _addOns.catalogs.statusOfAddingCatalog;
100 if (status == FG.CatalogListModel.Refreshing) {
101 // this can happen when adding the default catalog,
102 // ensure our state matches
103 addCatalogPanel.state = "add-in-progress"
104 return;
105 }
106
107 if (status == FG.CatalogListModel.NoAddInProgress) {
108 addCatalogPanel.state = "start";
109 return;
110 }
111
112 if (status == FG.CatalogListModel.Ok) {
113 // success, we are done
114 _addOns.catalogs.finalizeAddCatalog();
115 addCatalogPanel.state = "start";
116 return;
117 }
118
119 // if we reach this point, we're in an error situation, so
120 // handle that
121 addCatalogPanel.state = "add-failed-retry";
122 }
123 }
124
125 function addErrorMessage()
126 {
127 switch (_addOns.catalogs.statusOfAddingCatalog) {
128 case FG.CatalogListModel.NotFoundOnServer:
129 return qsTr("Failed to find a hangar description at the URL: '%1'. " +
130 "Check you entered the URL correctly.");
131 case FG.CatalogListModel.HTTPForbidden:
132 return qsTr("Access to the hangar data was forbidden by the server. " +
133 "Please check the URL you entered, or contact the hangar authors.");
134 case FG.CatalogListModel.NetworkError:
135 return qsTr("Failed to download from the server due to a network problem. " +
136 "Check your Internet connection is working, and that you entered the correct URL.");
137 case FG.CatalogListModel.IncomaptbleVersion:
138 return qsTr("The hangar you requested is for a different version of FlightGear. "
139 + "(This is version %1)").arg(_launcher.versionString);
140
141 case FG.CatalogListModel.InvalidData:
142 return qsTr("The requested URL doesn't contain valid hangar data. "
143 + "Check you entered a valid hangar URL. If it's correct, "
144 + "please contact the hangar authors, or try again later." );
145
146 default:
147 return qsTr("Unknown error: " + _addOns.catalogs.statusOfAddingCatalog);
148 }
149 }
150
151 states: [
152 State {
153 name: "start"
154 PropertyChanges { target: addCatalogPromptText; text: qsTr("Click here to add a new aircraft hangar. (Note this requires an Internet connection)"); clickable: true }
155 PropertyChanges { target: addCatalogUrlEntry; visible: false; text: "" } // reset URL on cancel / success
156 PropertyChanges { target: buttonBox; visible: false }
157 PropertyChanges { target: spinner; visible: false }
158 },
159
160 State {
161 name: "enter-url"
162 PropertyChanges { target: addCatalogPromptText; text: qsTr("Enter a hangar location (URL) to add."); clickable: false }
163 PropertyChanges { target: addCatalogUrlEntry; visible: true }
164 PropertyChanges { target: buttonBox; visible: true }
165 PropertyChanges { target: spinner; visible: false }
166 },
167
168 State {
169 name: "add-in-progress"
170 PropertyChanges { target: addCatalogPromptText; text: qsTr("Retrieving hangar information..."); clickable: false }
171 PropertyChanges { target: addCatalogUrlEntry; visible: false }
172 PropertyChanges { target: addCatalogProceedButton; enabled: false }
173 PropertyChanges { target: spinner; visible: true }
174 },
175
176
177 State {
178 name: "add-failed-retry"
179 PropertyChanges { target: addCatalogPromptText; text: qsTr("There was a problem adding the hangar: %1.").arg(addErrorMessage()); clickable: false }
180 PropertyChanges { target: addCatalogUrlEntry; visible: true }
181 PropertyChanges { target: addCatalogProceedButton; enabled: true }
182 PropertyChanges { target: spinner; visible: false }
183 }
184 ]
185 }