void UserHandler::handleMessage(string message) { string key; message = split_message(&key, message); try { try { UserHandler::viewmap.at (key)(JsonValue::fromString(message), this); } catch (const std::out_of_range & oor) { throw BadRequest("Unknown topic : '" + key + "'"); } } catch (const BadRequest & br) { JsonDict answer; answer.add("Bad request", new JsonString(br.what())); answer.add("code", new JsonInt(100)); this->writeToClient("error", &answer); } catch (const ParseError & pe) { JsonDict answer; answer.add("Parse error", new JsonInt(101)); answer.add("code", new JsonString(pe.what())); this->writeToClient("error", &answer); } }
Manager::operator JsonDict() const { JsonDict r; r.add("name", new JsonString(name_)); r.add("username", new JsonString(userName_)); r.add("hash", new JsonString(hash_)); JsonValue * club = new JsonDict(club_); r.add("club", club); return r; }
Team::operator JsonDict() const { JsonDict r; JsonList * players = new JsonList(); for (int i = 0; i < 7; i++) { if (players_[i] != NULL) { JsonDict * item = new JsonDict(*(players_[i])); players->add(item); } else { players->add(new JsonNull()); } } r.add("players", players); return r; }
JsonDict * JsonDict::fromString(string message, int & i) { i++; JsonDict * r = new JsonDict(); i += skip_whitespace(message, i); if (message[i] == '}') { i++; return r; } while (1) { JsonString * key = NULL; JsonValue * value = NULL; i += skip_whitespace(message, i); key = JsonString::fromString(message, i); string key_str = *key; delete key; i += skip_whitespace(message, i); i += skip_colon(message, i); i += skip_whitespace(message, i); value = JsonValue::fromString(message, i); r->add(key_str, value); i += skip_whitespace(message, i); if (message[i] == ',') { i++; } else if (message[i] == '}') { i++; return r; } else { throw PARSE_ERROR("expected } or , found '" + string(1, message[i]) + "'", i); } } }
QString prefjson::getPreferences() { const Preferences pref; JsonDict data; // UI data.add("locale", pref.getLocale()); // Downloads data.add("save_path", pref.getSavePath()); data.add("temp_path_enabled", pref.isTempPathEnabled()); data.add("temp_path", pref.getTempPath()); data.add("scan_dirs", pref.getScanDirs()); QVariantList var_list; foreach (bool b, pref.getDownloadInScanDirs()) { var_list << b; } data.add("download_in_scan_dirs", var_list); data.add("export_dir_enabled", pref.isTorrentExportEnabled()); data.add("export_dir", pref.getTorrentExportDir()); data.add("mail_notification_enabled", pref.isMailNotificationEnabled()); data.add("mail_notification_email", pref.getMailNotificationEmail()); data.add("mail_notification_smtp", pref.getMailNotificationSMTP()); data.add("mail_notification_ssl_enabled", pref.getMailNotificationSMTPSSL()); data.add("mail_notification_auth_enabled", pref.getMailNotificationSMTPAuth()); data.add("mail_notification_username", pref.getMailNotificationSMTPUsername()); data.add("mail_notification_password", pref.getMailNotificationSMTPPassword()); data.add("autorun_enabled", pref.isAutoRunEnabled()); data.add("autorun_program", pref.getAutoRunProgram()); data.add("preallocate_all", pref.preAllocateAllFiles()); data.add("queueing_enabled", pref.isQueueingSystemEnabled()); data.add("max_active_downloads", pref.getMaxActiveDownloads()); data.add("max_active_torrents", pref.getMaxActiveTorrents()); data.add("max_active_uploads", pref.getMaxActiveUploads()); data.add("dont_count_slow_torrents", pref.ignoreSlowTorrentsForQueueing()); data.add("incomplete_files_ext", pref.useIncompleteFilesExtension()); // Connection data.add("listen_port", pref.getSessionPort()); data.add("upnp", pref.isUPnPEnabled()); data.add("dl_limit", pref.getGlobalDownloadLimit()); data.add("up_limit", pref.getGlobalUploadLimit()); data.add("max_connec", pref.getMaxConnecs()); data.add("max_connec_per_torrent", pref.getMaxConnecsPerTorrent()); data.add("max_uploads_per_torrent", pref.getMaxUploadsPerTorrent()); data.add("enable_utp", pref.isuTPEnabled()); data.add("limit_utp_rate", pref.isuTPRateLimited()); data.add("limit_tcp_overhead", pref.includeOverheadInLimits()); data.add("alt_dl_limit", pref.getAltGlobalDownloadLimit()); data.add("alt_up_limit", pref.getAltGlobalUploadLimit()); data.add("scheduler_enabled", pref.isSchedulerEnabled()); const QTime start_time = pref.getSchedulerStartTime(); data.add("schedule_from_hour", start_time.hour()); data.add("schedule_from_min", start_time.minute()); const QTime end_time = pref.getSchedulerEndTime(); data.add("schedule_to_hour", end_time.hour()); data.add("schedule_to_min", end_time.minute()); data.add("scheduler_days", pref.getSchedulerDays()); // Bittorrent data.add("dht", pref.isDHTEnabled()); data.add("dhtSameAsBT", pref.isDHTPortSameAsBT()); data.add("dht_port", pref.getDHTPort()); data.add("pex", pref.isPeXEnabled()); data.add("lsd", pref.isLSDEnabled()); data.add("encryption", pref.getEncryptionSetting()); data.add("anonymous_mode", pref.isAnonymousModeEnabled()); // Proxy data.add("proxy_type", pref.getProxyType()); data.add("proxy_ip", pref.getProxyIp()); data.add("proxy_port", pref.getProxyPort()); data.add("proxy_peer_connections", pref.proxyPeerConnections()); data.add("proxy_auth_enabled", pref.isProxyAuthEnabled()); data.add("proxy_username", pref.getProxyUsername()); data.add("proxy_password", pref.getProxyPassword()); // IP Filter data.add("ip_filter_enabled", pref.isFilteringEnabled()); data.add("ip_filter_path", pref.getFilter()); // Web UI data.add("web_ui_port", pref.getWebUiPort()); data.add("web_ui_username", pref.getWebUiUsername()); data.add("web_ui_password", pref.getWebUiPassword()); data.add("bypass_local_auth", !pref.isWebUiLocalAuthEnabled()); data.add("use_https", pref.isWebUiHttpsEnabled()); data.add("ssl_key", QString::fromAscii(pref.getWebUiHttpsKey())); data.add("ssl_cert", QString::fromAscii(pref.getWebUiHttpsCertificate())); // DynDns data.add("dyndns_enabled", pref.isDynDNSEnabled()); data.add("dyndns_service", pref.getDynDNSService()); data.add("dyndns_username", pref.getDynDNSUsername()); data.add("dyndns_password", pref.getDynDNSPassword()); data.add("dyndns_domain", pref.getDynDomainName()); return data.toString(); }
static JsonDict toJson(const QTorrentHandle& h) { JsonDict ret; ret.add(KEY_TORRENT_HASH, h.hash()); ret.add(KEY_TORRENT_NAME, h.name()); ret.add(KEY_TORRENT_SIZE, misc::friendlyUnit(h.actual_size())); // FIXME: Should pass as Number, not formatted String (for sorting). ret.add(KEY_TORRENT_PROGRESS, (double)h.progress()); ret.add(KEY_TORRENT_DLSPEED, misc::friendlyUnit(h.download_payload_rate(), true)); // FIXME: Should be passed as a Number ret.add(KEY_TORRENT_UPSPEED, misc::friendlyUnit(h.upload_payload_rate(), true)); // FIXME: Should be passed as a Number if (QBtSession::instance()->isQueueingEnabled() && h.queue_position() >= 0) ret.add(KEY_TORRENT_PRIORITY, QString::number(h.queue_position())); else ret.add(KEY_TORRENT_PRIORITY, "*"); QString seeds = QString::number(h.num_seeds()); if (h.num_complete() > 0) seeds += " ("+QString::number(h.num_complete())+")"; ret.add(KEY_TORRENT_SEEDS, seeds); QString leechs = QString::number(h.num_peers() - h.num_seeds()); if (h.num_incomplete() > 0) leechs += " ("+QString::number(h.num_incomplete())+")"; ret.add(KEY_TORRENT_LEECHS, leechs); const qreal ratio = QBtSession::instance()->getRealRatio(h.hash()); /* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9 ** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 */ ret.add(KEY_TORRENT_RATIO, (ratio > 100.) ? QString::fromUtf8("∞") : QString::number((int)(ratio*10)/10.0, 'f', 1)); QString eta; QString state; if (h.is_paused()) { if (h.has_error()) state = "error"; else state = h.is_seed() ? "pausedUP" : "pausedDL"; } else { if (QBtSession::instance()->isQueueingEnabled() && h.is_queued()) state = h.is_seed() ? "queuedUP" : "queuedDL"; else { switch (h.state()) { case torrent_status::finished: case torrent_status::seeding: state = h.upload_payload_rate() > 0 ? "uploading" : "stalledUP"; break; case torrent_status::allocating: case torrent_status::checking_files: case torrent_status::queued_for_checking: case torrent_status::checking_resume_data: state = h.is_seed() ? "checkingUP" : "checkingDL"; break; case torrent_status::downloading: case torrent_status::downloading_metadata: state = h.download_payload_rate() > 0 ? "downloading" : "stalledDL"; eta = misc::userFriendlyDuration(QBtSession::instance()->getETA(h.hash())); break; default: qWarning("Unrecognized torrent status, should not happen!!! status was %d", h.state()); } } } ret.add(KEY_TORRENT_ETA, eta.isEmpty() ? QString::fromUtf8("∞") : eta); ret.add(KEY_TORRENT_STATE, state); return ret; }