summaryrefslogtreecommitdiff
path: root/src/osmEdit.js
blob: e4fa02d6dc5707268b8c78877ad7b3eb312d8faa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2015 Marcus Lundblad
 *
 * GNOME Maps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * GNOME Maps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNOME Maps; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: Marcus Lundblad <ml@update.uu.se>
 */

import {Application} from './application.js';
import {OSMAccountDialog} from './osmAccountDialog.js';
import {OSMEditDialog} from './osmEditDialog.js';
import {OSMConnection} from './osmConnection.js';
import * as Utils from './utils.js';

export class OSMEdit {

    // minimum zoom level at which to offer adding a location
    static get MIN_ADD_LOCATION_ZOOM_LEVEL() { return 16; }

    constructor() {
        this._osmConnection = new OSMConnection();
        this._osmObject = null; // currently edited object
        this._username = Application.settings.get('osm-username-oauth2');
        this._isSignedIn = this._username !== null && this._username.length > 0;
    }

    get object() {
        return this._osmObject;
    }

    createEditDialog(parentWindow, place) {
        let dialog = new OSMEditDialog({ transient_for: parentWindow,
                                         modal: true,
                                         place: place });

        return dialog;
    }

    createEditNewDialog(parentWindow, latitude, longitude) {
        let dialog = new OSMEditDialog({ transient_for: parentWindow,
                                         modal: true,
                                         addLocation: true,
                                         latitude: latitude,
                                         longitude: longitude });

        return dialog;
    }

    createAccountDialog(parentWindow, closeOnSignIn) {
        let dialog = new OSMAccountDialog({
            transient_for: parentWindow,
            modal: true,
            closeOnSignIn: closeOnSignIn
        });

        return dialog;
    }

    fetchObject(place, callback, cancellable) {
        let osmType = Utils.osmTypeToString(place.osmType);

        /* reset currently edited object */
        this._osmObject = null;
        this._osmConnection.getOSMObject(osmType, place.osm_id,
                     (function(success, status, osmObject, osmType) {
                         callback(success, status, osmObject, osmType);
                     }), cancellable);
    }

    uploadObject(object, type, comment, callback) {
        this._openChangeset(object, type, comment,
                            this._uploadObject.bind(this), callback);
    }

    _onChangesetOpened(success, status, changesetId, object, type, action, callback) {
        if (success) {
            let osmType = Utils.osmTypeToString(type);
            action(object, osmType, changesetId, callback);
        } else {
            callback(false, status);
        }
    }

    _openChangeset(object, type, comment, action, callback) {
        this._osmConnection.openChangeset(comment, (success, status, changesetId) => {
            this._onChangesetOpened(success, status, changesetId, object, type, action, callback);
        });
    }

    _onObjectUploaded(success, status, response, changesetId, callback) {
        if (success)
            this._closeChangeset(changesetId, callback);
        else
            callback(false, status);
    }

    _uploadObject(object, type, changesetId, callback) {
        this._osmObject = object;
        this._osmConnection.uploadObject(object, type, changesetId, (success, status, response) => {
            this._onObjectUploaded(success, status, response, changesetId, callback);
        });
    }

    deleteObject(object, type, comment, callback) {
        this._openChangeset(object, type, comment,
                            this._deleteObject.bind(this), callback);
    }

    _onObjectDeleted(success, status, response, changesetId, callback) {
        if (success)
            this._closeChangeset(changesetId, callback);
        else
            callback(false, status);
    }

    _deleteObject(object, type, changesetId, callback) {
        this._osmObject = object;
        this._osmConnection.deleteObject(object, type, changesetId, (success, status, response) => {
            this._onObjectDeleted(success, status, response, changesetId, callback);
        });
    }

    _closeChangeset(changesetId, callback) {
        this._osmConnection.closeChangeset(changesetId, callback);
    }

    performOAuthSignIn() {
        this._osmConnection.authorizeOAuthToken();
    }

    requestOAuthAccessToken(code, callback) {
        this._osmConnection.requestOAuthAccessToken(code, (success, token) => {
            this._onOAuthAccessTokenRequested(success, callback);
        });
    }

    _onOAuthAccessTokenRequested(success, callback) {
        if (success) {
            this._osmConnection.fetchLoggedInUser((username) => {
                this._isSignedIn = true;
                /* if we couldn't retrieve the logged-in username,
                 * e.g. if the user de-selected the permission when
                 * authorizing the OAuth token, use a dummy placeholder
                 * username to signify that we are signed in
                 */
                this._username = username ?? '_unknown_';
                Application.settings.set('osm-username-oauth2', this._username);
                callback(true);
            });
        } else {
            callback(false);
        }
    }

    signOut() {
        this._username = null;
        this._isSignedIn = false;

        Application.settings.set('osm-username-oauth2', '');
        this._osmConnection.signOut();
    }

    get isSignedIn() {
        return this._isSignedIn;
    }

    get username() {
        return this._username;
    }
}