void Connection::load(const QVariantMap& m)
{
    setTitle(m["title"].toString());
    setApiUrl(m["apiUrl"].toString());
    setUserEmail(m["userEmail"].toString());
    setApiKey(m["apiKey"].toString());
}
Beispiel #2
0
void inbox_received_callback(DictionaryIterator *iterator, void *context) {
  // Read tuples for data
  Tuple *temp_tuple = dict_find(iterator, MESSAGE_KEY_TEMPERATURE);
  Tuple *conditions_tuple = dict_find(iterator, MESSAGE_KEY_CONDITION_DESC);

  // If all data is available, use it
  if(temp_tuple && conditions_tuple) {
    signalSuccessfulWeatherUpdate();
    updateWeather((int)temp_tuple->value->int32, conditions_tuple->value->cstring);
  }
  
  bool requestWeatherUpdate = false;
  Tuple *app_id_tuple = dict_find(iterator, MESSAGE_KEY_OWM_APPID);
  if (app_id_tuple) {
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Received config App-ID: %s", app_id_tuple->value->cstring);
    if (setApiKey(app_id_tuple->value->cstring)) {
      APP_LOG(APP_LOG_LEVEL_DEBUG, "API key initialized, requesting weather update.");
      requestWeatherUpdate = true;
    }
  }
  
  Tuple *js_ready_tuple = dict_find(iterator, MESSAGE_KEY_JS_KIT_READY);
  if (js_ready_tuple) {
    setJsReady();
    requestWeatherUpdate = true;
  }
  
  Tuple *update_frequency_tuple = dict_find(iterator, MESSAGE_KEY_UPDATE_FREQ);
  if (update_frequency_tuple) {
    int32_t frequency = update_frequency_tuple->value->int32;
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Received update frequency %i", (unsigned int)frequency);
    setUpdateFrequencyInMinutes((unsigned int)frequency);
    requestWeatherUpdate = true;
  }
  
  Tuple *unit_temp_tuple = dict_find(iterator, MESSAGE_KEY_UNIT_TEMP);
  if (unit_temp_tuple) {
    char *unit = unit_temp_tuple->value->cstring;
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Received %s as temperature unit.", unit);
    bool useCelsius = strncmp(unit, "C", 1) == 0;
    setTemperatureUnitToCelsius(useCelsius);
    requestWeatherUpdate = true;
  }
  
  Tuple *weather_locale_tuple = dict_find(iterator, MESSAGE_KEY_WEATHER_LOCALE);
  if (weather_locale_tuple) {
    int32_t localeAsInt = weather_locale_tuple->value->int32;
    APP_LOG(APP_LOG_LEVEL_DEBUG, "Received %i as locale.", (unsigned int)localeAsInt);
    setWeatherLocale(localeAsInt);
    requestWeatherUpdate = true;
  }
  
  
  if (requestWeatherUpdate) {
    checkWeatherUpdate();
  }
}
QNetworkReply* Connection::connectToAccount(const QString &email,
                                            const QString &password,
                                            PMS::SimpleResultHandler handler)
{
    QUrlQuery query;
    query.addQueryItem("api_subscription[email]", email);
    query.addQueryItem("api_subscription[password]", password);
    query.addQueryItem("api_subscription[client_name]", qApp->applicationName());
    query.addQueryItem("api_subscription[client_vendor]", qApp->organizationName());
    QNetworkRequest request(apiUrl_);
    request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/x-www-form-urlencoded");
    auto reply = client_->post(request, query.toString().toUtf8());

    QPointer<QObject> self = this;
    QObject::connect(reply, &QNetworkReply::finished, [self, this, reply, handler]()
    {
        if (!self)
            return;
        try
        {
            checkReply(reply);
            auto str = reply->readAll().trimmed();

            QRegExp apiKeyRx("^API key: (.+)$");
            if (apiKeyRx.exactMatch(str))
            {
                setApiKey(apiKeyRx.cap(1));
                handler(Error());
                return;
            }

            QRegExp errorRx("^Error Code: (\\d+)$");
            if (errorRx.exactMatch(str))
            {
                int code = errorRx.cap(1).toInt();
                QString error;
                switch (code)
                {
                    case 1: error = tr("Client details not set"); break;
                    case 2: error = tr("Unknown user"); break;
                    case 3: error = tr("Invalid password"); break;
                    case 4: error = tr("Not allowed for given User and their System Role"); break;
                    default: error = tr("Unknown error"); break;
                }
                throw Error(Error::Authorization, error);
            }

            throw Error(Error::Authorization, tr("Unknown error"));
        }
        catch (const Error& err)
        {
            handler(err);
        }
    });
}