Commit 0aed5e4eb3af56ba0bf9ceb0343c262a7427b6ba
- Diff rendering mode:
- inline
- side by side
src/CMakeLists.txt
(2 / 0)
|   | |||
| 259 | 259 | net/adminhandler.h | |
| 260 | 260 | net/charhandler.h | |
| 261 | 261 | net/chathandler.h | |
| 262 | net/download.cpp | ||
| 263 | net/download.h | ||
| 262 | 264 | net/gamehandler.h | |
| 263 | 265 | net/generalhandler.h | |
| 264 | 266 | net/guildhandler.h |
src/Makefile.am
(2 / 0)
|   | |||
| 208 | 208 | net/adminhandler.h \ | |
| 209 | 209 | net/charhandler.h \ | |
| 210 | 210 | net/chathandler.h \ | |
| 211 | net/download.cpp \ | ||
| 212 | net/download.h \ | ||
| 211 | 213 | net/gamehandler.h \ | |
| 212 | 214 | net/generalhandler.h \ | |
| 213 | 215 | net/guildhandler.h \ |
src/gui/serverdialog.cpp
(197 / 126)
|   | |||
| 36 | 36 | ||
| 37 | 37 | #include "net/net.h" | |
| 38 | 38 | ||
| 39 | #include "utils/xml.h" | ||
| 40 | 39 | #include "utils/gettext.h" | |
| 41 | 40 | #include "utils/stringutils.h" | |
| 41 | #include "utils/xml.h" | ||
| 42 | 42 | ||
| 43 | 43 | #include <cstdlib> | |
| 44 | 44 | #include <iostream> | |
| 45 | 45 | #include <string> | |
| 46 | 46 | ||
| 47 | const short MAX_SERVERLIST = 5; | ||
| 47 | #define MAX_SERVERLIST 5 | ||
| 48 | 48 | ||
| 49 | int ServersListModel::getNumberOfElements() | ||
| 49 | ServersListModel::ServersListModel(ServerInfos *servers, ServerDialog *parent): | ||
| 50 | mServers(servers), | ||
| 51 | mParent(parent) | ||
| 50 | 52 | { | |
| 51 | return servers.size(); | ||
| 52 | 53 | } | |
| 53 | 54 | ||
| 54 | std::string ServersListModel::getElementAt(int elementIndex) | ||
| 55 | int ServersListModel::getNumberOfElements() | ||
| 55 | 56 | { | |
| 56 | std::string myServer = servers.at(elementIndex).name; | ||
| 57 | myServer += " ("; | ||
| 58 | myServer += std::string(servers.at(elementIndex).hostname); | ||
| 59 | myServer += ":"; | ||
| 60 | myServer += toString(servers.at(elementIndex).port); | ||
| 61 | myServer += ")"; | ||
| 62 | return myServer; | ||
| 57 | MutexLocker lock = mParent->lock(); | ||
| 58 | return mServers->size(); | ||
| 63 | 59 | } | |
| 64 | 60 | ||
| 65 | void ServersListModel::addFirstElement(const ServerInfo &server) | ||
| 61 | std::string ServersListModel::getElementAt(int elementIndex) | ||
| 66 | 62 | { | |
| 67 | // Equivalent to push_front | ||
| 68 | std::vector<ServerInfo>::iterator MyIterator = servers.begin(); | ||
| 69 | servers.insert(MyIterator, 1, server); | ||
| 70 | } | ||
| 71 | |||
| 72 | void ServersListModel::addElement(const ServerInfo &server) | ||
| 73 | { | ||
| 74 | servers.push_back(server); | ||
| 75 | } | ||
| 76 | |||
| 77 | void ServersListModel::mergeElement(const ServerInfo &server) | ||
| 78 | { | ||
| 79 | // search through the list | ||
| 80 | for (int i = 0; i < getNumberOfElements(); i++) | ||
| 63 | MutexLocker lock = mParent->lock(); | ||
| 64 | ServerInfo server = mServers->at(elementIndex); | ||
| 65 | std::string myServer; | ||
| 66 | if (server.name.empty()) | ||
| 81 | 67 | { | |
| 82 | // the server is already in the list, merge its properties | ||
| 83 | if (servers[i] == server) | ||
| 84 | { | ||
| 85 | servers[i].name = server.name; | ||
| 86 | return; | ||
| 87 | } | ||
| 68 | myServer += server.hostname; | ||
| 69 | myServer += ":"; | ||
| 70 | myServer += toString(server.port); | ||
| 88 | 71 | } | |
| 89 | // the server is not found, add it at the end of the list | ||
| 90 | addElement(server); | ||
| 91 | } | ||
| 92 | |||
| 93 | bool ServersListModel::contains(const ServerInfo &server) | ||
| 94 | { | ||
| 95 | // search through the list | ||
| 96 | for (int i = 0; i < getNumberOfElements(); i++) | ||
| 72 | else | ||
| 97 | 73 | { | |
| 98 | if (servers[i] == server) | ||
| 99 | return true; | ||
| 74 | myServer += server.name; | ||
| 75 | myServer += " ("; | ||
| 76 | myServer += server.hostname; | ||
| 77 | myServer += ":"; | ||
| 78 | myServer += toString(server.port); | ||
| 79 | myServer += ")"; | ||
| 100 | 80 | } | |
| 101 | return false; | ||
| 81 | return myServer; | ||
| 102 | 82 | } | |
| 103 | 83 | ||
| 104 | 84 | ||
| 105 | ServerDialog::ServerDialog(ServerInfo *serverInfo): | ||
| 106 | Window(_("Choose Your Server")), mServerInfo(serverInfo) | ||
| 85 | ServerDialog::ServerDialog(ServerInfo *serverInfo, const std::string &dir): | ||
| 86 | Window(_("Choose Your Server")), | ||
| 87 | mDir(dir), | ||
| 88 | mDownloadStatus(DOWNLOADING_PREPARING), | ||
| 89 | mDownloadProgress(-1.0f), | ||
| 90 | mServers(ServerInfos()), | ||
| 91 | mServerInfo(serverInfo) | ||
| 107 | 92 | { | |
| 108 | mServerDescription = new Label(std::string()); | ||
| 109 | gcn::Label *serverLabel = new Label(_("Server:")); | ||
| 110 | gcn::Label *portLabel = new Label(_("Port:")); | ||
| 93 | Label *serverLabel = new Label(_("Server:")); | ||
| 94 | Label *portLabel = new Label(_("Port:")); | ||
| 111 | 95 | mServerNameField = new TextField(mServerInfo->hostname); | |
| 112 | 96 | mPortField = new TextField(toString(mServerInfo->port)); | |
| 113 | 97 | ||
| 114 | mMostUsedServersListModel = new ServersListModel; | ||
| 115 | 98 | ServerInfo currentServer; | |
| 116 | ServerInfo tempServer; | ||
| 117 | |||
| 118 | 99 | // Add the most used servers from config if they are not in the online list | |
| 119 | 100 | std::string currentConfig = ""; | |
| 120 | 101 | for (int i = 0; i <= MAX_SERVERLIST; i++) | |
| … | … | ||
| 110 | 110 | ||
| 111 | 111 | if (!currentServer.hostname.empty() && currentServer.port != 0) | |
| 112 | 112 | { | |
| 113 | if (!mMostUsedServersListModel->contains(currentServer)) | ||
| 114 | mMostUsedServersListModel->addElement(currentServer); | ||
| 113 | mServers.push_back(currentServer); | ||
| 115 | 114 | } | |
| 116 | 115 | } | |
| 117 | 116 | ||
| 118 | // load a list with online servers... | ||
| 119 | loadServerlist(); | ||
| 117 | mServersListModel = new ServersListModel(&mServers, this); | ||
| 120 | 118 | ||
| 121 | mMostUsedServersList = new ListBox(mMostUsedServersListModel); | ||
| 122 | ScrollArea *usedScroll = new ScrollArea(mMostUsedServersList); | ||
| 119 | mServersList = new ListBox(mServersListModel); | ||
| 120 | ScrollArea *usedScroll = new ScrollArea(mServersList); | ||
| 123 | 121 | usedScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); | |
| 124 | 122 | ||
| 123 | mDescription = new Label(std::string()); | ||
| 124 | |||
| 125 | 125 | mQuitButton = new Button(_("Quit"), "quit", this); | |
| 126 | 126 | mConnectButton = new Button(_("Connect"), "connect", this); | |
| 127 | 127 | mManualEntryButton = new Button(_("Add Entry"), "addEntry", this); | |
| … | … | ||
| 132 | 132 | mServerNameField->addActionListener(this); | |
| 133 | 133 | mPortField->addActionListener(this); | |
| 134 | 134 | mManualEntryButton->addActionListener(this); | |
| 135 | mMostUsedServersList->addSelectionListener(this); | ||
| 136 | mMostUsedServersList->setSelected(0); | ||
| 135 | mServersList->addSelectionListener(this); | ||
| 136 | mServersList->setSelected(0); | ||
| 137 | 137 | usedScroll->setVerticalScrollAmount(0); | |
| 138 | 138 | ||
| 139 | place(0, 0, mServerDescription, 2); | ||
| 140 | place(0, 1, serverLabel); | ||
| 141 | place(0, 2, portLabel); | ||
| 142 | place(1, 1, mServerNameField, 3).setPadding(3); | ||
| 143 | place(1, 2, mPortField, 3).setPadding(3); | ||
| 144 | place(0, 3, usedScroll, 4, 5).setPadding(3); | ||
| 139 | place(0, 0, serverLabel); | ||
| 140 | place(1, 0, mServerNameField, 3).setPadding(3); | ||
| 141 | place(0, 1, portLabel); | ||
| 142 | place(1, 1, mPortField, 3).setPadding(3); | ||
| 143 | place(0, 2, usedScroll, 4, 5).setPadding(3); | ||
| 144 | place(0, 7, mDescription, 4); | ||
| 145 | 145 | place(0, 8, mManualEntryButton); | |
| 146 | 146 | place(2, 8, mQuitButton); | |
| 147 | 147 | place(3, 8, mConnectButton); | |
| … | … | ||
| 166 | 166 | } | |
| 167 | 167 | } | |
| 168 | 168 | ||
| 169 | downloadServerList(); | ||
| 169 | 170 | } | |
| 170 | 171 | ||
| 171 | 172 | ServerDialog::~ServerDialog() | |
| 172 | 173 | { | |
| 173 | delete mMostUsedServersListModel; | ||
| 174 | delete mServersListModel; | ||
| 174 | 175 | } | |
| 175 | 176 | ||
| 176 | 177 | void ServerDialog::action(const gcn::ActionEvent &event) | |
| … | … | ||
| 209 | 209 | // now add the rest of the list... | |
| 210 | 210 | std::string currentConfig = ""; | |
| 211 | 211 | int configCount = 1; | |
| 212 | for (int i = 0; i < mMostUsedServersListModel->getNumberOfElements(); i++) | ||
| 212 | for (int i = 0; i < mServersListModel->getNumberOfElements(); i++) | ||
| 213 | 213 | { | |
| 214 | tempServer = mMostUsedServersListModel->getServer(i); | ||
| 214 | tempServer = mServersListModel->getServer(i); | ||
| 215 | 215 | ||
| 216 | 216 | // ensure, that our server will not be added twice | |
| 217 | 217 | if (tempServer != currentServer) | |
| … | … | ||
| 244 | 244 | ||
| 245 | 245 | void ServerDialog::valueChanged(const gcn::SelectionEvent &event) | |
| 246 | 246 | { | |
| 247 | const int index = mMostUsedServersList->getSelected(); | ||
| 247 | const int index = mServersList->getSelected(); | ||
| 248 | 248 | if (index == -1) | |
| 249 | 249 | return; | |
| 250 | 250 | ||
| 251 | 251 | // Update the server and post fields according to the new selection | |
| 252 | const ServerInfo myServer = mMostUsedServersListModel->getServer(index); | ||
| 253 | mServerDescription->setCaption(myServer.name); | ||
| 252 | const ServerInfo myServer = mServersListModel->getServer(index); | ||
| 253 | mDescription->setCaption(myServer.name); | ||
| 254 | 254 | mServerNameField->setText(myServer.hostname); | |
| 255 | 255 | mPortField->setText(toString(myServer.port)); | |
| 256 | 256 | ||
| 257 | 257 | setFieldsReadOnly(true); | |
| 258 | 258 | } | |
| 259 | 259 | ||
| 260 | void ServerDialog::loadServerlist() | ||
| 260 | void ServerDialog::logic() | ||
| 261 | 261 | { | |
| 262 | ServerInfo currentServer; | ||
| 263 | currentServer.clear(); | ||
| 262 | { | ||
| 263 | MutexLocker lock(&mMutex); | ||
| 264 | if (mDownloadStatus == DOWNLOADING_COMPLETE) | ||
| 265 | { | ||
| 266 | mDownloadStatus = DOWNLOADING_OVER; | ||
| 264 | 267 | ||
| 268 | mDescription->setCaption(std::string()); | ||
| 269 | } | ||
| 270 | else if (mDownloadStatus == DOWNLOADING_IN_PROGRESS) | ||
| 271 | { | ||
| 272 | mDescription->setCaption(strprintf(_("Downloading server list..." | ||
| 273 | "%2.2f%%"), | ||
| 274 | mDownloadProgress * 100)); | ||
| 275 | } | ||
| 276 | else if (mDownloadStatus == DOWNLOADING_IDLE) | ||
| 277 | { | ||
| 278 | mDescription->setCaption(_("Waiting for server...")); | ||
| 279 | } | ||
| 280 | else if (mDownloadStatus == DOWNLOADING_PREPARING) | ||
| 281 | { | ||
| 282 | mDescription->setCaption(_("Preparing download")); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | Window::logic(); | ||
| 287 | } | ||
| 288 | |||
| 289 | void ServerDialog::setFieldsReadOnly(const bool readOnly) | ||
| 290 | { | ||
| 291 | if (readOnly) | ||
| 292 | { | ||
| 293 | mServerNameField->setEnabled(false); | ||
| 294 | mPortField->setEnabled(false); | ||
| 295 | mManualEntryButton->setVisible(true); | ||
| 296 | mDescription->setVisible(true); | ||
| 297 | } | ||
| 298 | else | ||
| 299 | { | ||
| 300 | mManualEntryButton->setVisible(false); | ||
| 301 | |||
| 302 | mDescription->setVisible(false); | ||
| 303 | mDescription->setCaption(std::string()); | ||
| 304 | mServersList->setSelected(-1); | ||
| 305 | |||
| 306 | mServerNameField->setText(std::string()); | ||
| 307 | mServerNameField->setEnabled(true); | ||
| 308 | |||
| 309 | mPortField->setText(toString(DEFAULT_PORT)); | ||
| 310 | mPortField->setEnabled(true); | ||
| 311 | |||
| 312 | mServerNameField->requestFocus(); | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | void ServerDialog::downloadServerList() | ||
| 317 | { | ||
| 265 | 318 | // try to load the configuration value for the onlineServerList | |
| 266 | 319 | std::string listFile = config.getValue("onlineServerList", "void"); | |
| 267 | 320 | // if there is no entry, try to load the file from the default updatehost | |
| … | … | ||
| 322 | 322 | listFile = config.getValue("updatehost", "http://updates.themanaworld.org") | |
| 323 | 323 | + "/serverlist.xml"; | |
| 324 | 324 | ||
| 325 | xmlDocPtr doc = xmlReadFile(listFile.c_str(), NULL, 0); | ||
| 326 | if (doc == NULL) | ||
| 327 | { | ||
| 328 | logger->log("Failed to load online serverlist from %s", listFile.c_str()); | ||
| 329 | return; | ||
| 330 | } | ||
| 325 | mDownload = new Net::Download(this, listFile, &downloadUpdate); | ||
| 326 | mDownload->setFile(mDir + "/serverlist.xml"); | ||
| 327 | mDownload->start(); | ||
| 328 | } | ||
| 331 | 329 | ||
| 332 | xmlNodePtr rootNode = xmlDocGetRootElement(doc); | ||
| 333 | int version = XML::getProperty(rootNode, "version", 3); | ||
| 330 | void ServerDialog::loadServers() | ||
| 331 | { | ||
| 332 | ServerInfo currentServer; | ||
| 334 | 333 | ||
| 335 | if (version != 1) | ||
| 336 | { | ||
| 337 | logger->log("Online server list has wrong version"); | ||
| 338 | return; | ||
| 339 | } | ||
| 334 | xmlDocPtr doc = xmlReadFile((mDir + "/serverlist.xml").c_str(), NULL, 0); | ||
| 340 | 335 | ||
| 341 | for_each_xml_child_node(server, rootNode) | ||
| 336 | if (doc != NULL) | ||
| 342 | 337 | { | |
| 343 | if (xmlStrEqual(server->name, BAD_CAST "server")) | ||
| 338 | xmlNodePtr rootNode = xmlDocGetRootElement(doc); | ||
| 339 | int version = XML::getProperty(rootNode, "version", 3); | ||
| 340 | |||
| 341 | if (version != 1) | ||
| 344 | 342 | { | |
| 345 | //check wether the version matches | ||
| 346 | #ifdef TMWSERV_SUPPORT | ||
| 347 | if (XML::getProperty(server, "type", "unknown") != "TMWSERV") | ||
| 348 | continue; | ||
| 349 | #endif | ||
| 343 | logger->log("Online server list has wrong version"); | ||
| 344 | return; | ||
| 345 | } | ||
| 350 | 346 | ||
| 351 | #ifdef EATHENA_SUPPORT | ||
| 352 | if (XML::getProperty(server, "type", "unknown") != "EATHENA") | ||
| 353 | continue; | ||
| 354 | #endif | ||
| 347 | for_each_xml_child_node(server, rootNode) | ||
| 348 | { | ||
| 349 | if (xmlStrEqual(server->name, BAD_CAST "server")) | ||
| 350 | { | ||
| 351 | //check wether the version matches | ||
| 352 | #ifdef TMWSERV_SUPPORT | ||
| 353 | if (XML::getProperty(server, "type", "unknown") != "TMWSERV") | ||
| 354 | continue; | ||
| 355 | #endif | ||
| 355 | 356 | ||
| 356 | currentServer.clear(); | ||
| 357 | currentServer.name = XML::getProperty(server, "name", std::string()); | ||
| 357 | #ifdef EATHENA_SUPPORT | ||
| 358 | if (XML::getProperty(server, "type", "unknown") != "EATHENA") | ||
| 359 | continue; | ||
| 360 | #endif | ||
| 358 | 361 | ||
| 359 | for_each_xml_child_node(subnode, server) | ||
| 360 | { | ||
| 361 | if (xmlStrEqual(subnode->name, BAD_CAST "connection")) | ||
| 362 | currentServer.clear(); | ||
| 363 | currentServer.name = XML::getProperty(server, "name", std::string()); | ||
| 364 | |||
| 365 | for_each_xml_child_node(subnode, server) | ||
| 362 | 366 | { | |
| 363 | currentServer.hostname = XML::getProperty(subnode, "hostname", std::string()); | ||
| 364 | currentServer.port = XML::getProperty(subnode, "port", DEFAULT_PORT); | ||
| 367 | if (xmlStrEqual(subnode->name, BAD_CAST "connection")) | ||
| 368 | { | ||
| 369 | currentServer.hostname = XML::getProperty(subnode, "hostname", std::string()); | ||
| 370 | currentServer.port = XML::getProperty(subnode, "port", DEFAULT_PORT); | ||
| 371 | } | ||
| 365 | 372 | } | |
| 366 | } | ||
| 367 | 373 | ||
| 368 | // merge the server into the local list | ||
| 369 | mMostUsedServersListModel->mergeElement(currentServer); | ||
| 374 | |||
| 375 | MutexLocker lock(&mMutex); | ||
| 376 | // add the server to the local list (if it's not already present) | ||
| 377 | ServerInfos::iterator it; | ||
| 378 | bool found = false; | ||
| 379 | for (it = mServers.begin(); it != mServers.end(); it++) | ||
| 380 | { | ||
| 381 | if ((*it) == currentServer) | ||
| 382 | { | ||
| 383 | (*it).name = currentServer.name; | ||
| 384 | found = true; | ||
| 385 | break; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | |||
| 389 | if (!found) | ||
| 390 | mServers.push_back(currentServer); | ||
| 391 | } | ||
| 370 | 392 | } | |
| 393 | |||
| 394 | xmlFreeDoc(doc); | ||
| 371 | 395 | } | |
| 372 | 396 | ||
| 373 | xmlFreeDoc(doc); | ||
| 397 | MutexLocker lock(&mMutex); | ||
| 398 | mDownloadStatus = DOWNLOADING_COMPLETE; | ||
| 374 | 399 | } | |
| 375 | 400 | ||
| 376 | void ServerDialog::setFieldsReadOnly(const bool readOnly) | ||
| 401 | int ServerDialog::downloadUpdate(void *ptr, DownloadStatus status, | ||
| 402 | size_t total, size_t remaining) | ||
| 377 | 403 | { | |
| 378 | if (readOnly) | ||
| 404 | ServerDialog *sd = reinterpret_cast<ServerDialog*>(ptr); | ||
| 405 | bool finished = false; | ||
| 406 | |||
| 407 | if (status == DOWNLOAD_STATUS_COMPLETE) | ||
| 379 | 408 | { | |
| 380 | mServerNameField->setEnabled(false); | ||
| 381 | mPortField->setEnabled(false); | ||
| 382 | mManualEntryButton->setVisible(true); | ||
| 383 | mServerDescription->setVisible(true); | ||
| 409 | finished = true; | ||
| 384 | 410 | } | |
| 411 | else if (status < 0) | ||
| 412 | { | ||
| 413 | logger->log("Error retreiving server list: %s\n", | ||
| 414 | sd->mDownload->getError()); | ||
| 415 | |||
| 416 | finished = true; | ||
| 417 | } | ||
| 385 | 418 | else | |
| 386 | 419 | { | |
| 387 | mManualEntryButton->setVisible(false); | ||
| 420 | float progress = (float) remaining / total; | ||
| 388 | 421 | ||
| 389 | mServerDescription->setVisible(false); | ||
| 390 | mServerDescription->setCaption(std::string()); | ||
| 391 | mMostUsedServersList->setSelected(-1); | ||
| 422 | if (progress != progress) progress = 0.0f; // check for NaN | ||
| 423 | if (progress < 0.0f) progress = 0.0f; // no idea how this could ever happen, but why not check for it anyway. | ||
| 424 | if (progress > 1.0f) progress = 1.0f; | ||
| 392 | 425 | ||
| 393 | mServerNameField->setText(std::string()); | ||
| 394 | mServerNameField->setEnabled(true); | ||
| 426 | MutexLocker lock(&sd->mMutex); | ||
| 427 | sd->mDownloadStatus = DOWNLOADING_IN_PROGRESS; | ||
| 428 | sd->mDownloadProgress = progress; | ||
| 429 | } | ||
| 395 | 430 | ||
| 396 | mPortField->setText(toString(DEFAULT_PORT)); | ||
| 397 | mPortField->setEnabled(true); | ||
| 398 | |||
| 399 | mServerNameField->requestFocus(); | ||
| 431 | if (finished) | ||
| 432 | { | ||
| 433 | sd->loadServers(); | ||
| 400 | 434 | } | |
| 435 | |||
| 436 | return 0; | ||
| 401 | 437 | } |
src/gui/serverdialog.h
(52 / 40)
|   | |||
| 24 | 24 | ||
| 25 | 25 | #include "gui/widgets/window.h" | |
| 26 | 26 | ||
| 27 | #include "guichanfwd.h" | ||
| 28 | |||
| 27 | #include "net/download.h" | ||
| 29 | 28 | #include "net/serverinfo.h" | |
| 30 | 29 | ||
| 30 | #include "utils/mutex.h" | ||
| 31 | |||
| 31 | 32 | #include <guichan/actionlistener.hpp> | |
| 32 | 33 | #include <guichan/listmodel.hpp> | |
| 33 | 34 | #include <guichan/selectionlistener.hpp> | |
| … | … | ||
| 36 | 36 | #include <string> | |
| 37 | 37 | #include <vector> | |
| 38 | 38 | ||
| 39 | class Button; | ||
| 40 | class Label; | ||
| 39 | 41 | class ListBox; | |
| 42 | class ServerDialog; | ||
| 43 | class TextField; | ||
| 40 | 44 | ||
| 41 | 45 | /** | |
| 42 | 46 | * Server and Port List Model | |
| … | … | ||
| 48 | 48 | class ServersListModel : public gcn::ListModel | |
| 49 | 49 | { | |
| 50 | 50 | public: | |
| 51 | ServersListModel(ServerInfos *servers, ServerDialog *parent); | ||
| 52 | |||
| 51 | 53 | /** | |
| 52 | 54 | * Used to get number of line in the list | |
| 53 | 55 | */ | |
| … | … | ||
| 64 | 64 | * Used to get the corresponding Server struct | |
| 65 | 65 | */ | |
| 66 | 66 | ServerInfo getServer(int elementIndex) const | |
| 67 | { return servers[elementIndex]; } | ||
| 67 | { return mServers->at(elementIndex); } | ||
| 68 | 68 | ||
| 69 | /** | ||
| 70 | * Add an Element at the end of the server list | ||
| 71 | */ | ||
| 72 | void addElement(const ServerInfo &server); | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Add an Element at the end of the server list if it | ||
| 76 | * doesn't exist yet. Otherwise overwrite its properties | ||
| 77 | * in the list. | ||
| 78 | * | ||
| 79 | * @param server ServerInfo to merge into the list. | ||
| 80 | */ | ||
| 81 | void mergeElement(const ServerInfo &server); | ||
| 82 | |||
| 83 | /** | ||
| 84 | * Add an Element at the beginning of the server list | ||
| 85 | */ | ||
| 86 | void addFirstElement(const ServerInfo &server); | ||
| 87 | |||
| 88 | /** | ||
| 89 | * Returns wheter the given server is already in the list. | ||
| 90 | * @param server Server to search in the list. | ||
| 91 | * @return True, if the server is in the list, false otherwise. | ||
| 92 | */ | ||
| 93 | bool contains(const ServerInfo &server); | ||
| 94 | |||
| 95 | 69 | private: | |
| 96 | std::vector<ServerInfo> servers; | ||
| 70 | ServerInfos *mServers; | ||
| 71 | ServerDialog *mParent; | ||
| 97 | 72 | }; | |
| 98 | 73 | ||
| 99 | 74 | /** | |
| … | … | ||
| 86 | 86 | * | |
| 87 | 87 | * @see Window::Window | |
| 88 | 88 | */ | |
| 89 | ServerDialog(ServerInfo *serverInfo); | ||
| 89 | ServerDialog(ServerInfo *serverInfo, const std::string &dir); | ||
| 90 | 90 | ||
| 91 | 91 | /** | |
| 92 | 92 | * Destructor | |
| … | … | ||
| 103 | 103 | */ | |
| 104 | 104 | void valueChanged(const gcn::SelectionEvent &event); | |
| 105 | 105 | ||
| 106 | void logic(); | ||
| 107 | |||
| 108 | protected: | ||
| 109 | friend class ServersListModel; | ||
| 110 | MutexLocker lock() { return MutexLocker(&mMutex); } | ||
| 111 | |||
| 106 | 112 | private: | |
| 107 | 113 | /** | |
| 108 | 114 | * Called to load a list of available server from an online xml file. | |
| 109 | 115 | */ | |
| 110 | void loadServerlist(); | ||
| 116 | void downloadServerList(); | ||
| 117 | void loadServers(); | ||
| 118 | static int downloadUpdate(void *ptr, DownloadStatus status, | ||
| 119 | size_t total, size_t remaining); | ||
| 111 | 120 | ||
| 112 | 121 | void setFieldsReadOnly(const bool readOnly); | |
| 113 | 122 | ||
| 114 | gcn::TextField *mServerNameField; | ||
| 115 | gcn::TextField *mPortField; | ||
| 116 | gcn::Label *mServerDescription; | ||
| 117 | gcn::Button *mQuitButton; | ||
| 118 | gcn::Button *mConnectButton; | ||
| 119 | gcn::Button *mManualEntryButton; | ||
| 123 | TextField *mServerNameField; | ||
| 124 | TextField *mPortField; | ||
| 125 | Label *mDescription; | ||
| 126 | Button *mQuitButton; | ||
| 127 | Button *mConnectButton; | ||
| 128 | Button *mManualEntryButton; | ||
| 120 | 129 | ||
| 121 | ListBox *mMostUsedServersList; | ||
| 122 | ServersListModel *mMostUsedServersListModel; | ||
| 130 | ListBox *mServersList; | ||
| 131 | ServersListModel *mServersListModel; | ||
| 123 | 132 | ||
| 133 | const std::string &mDir; | ||
| 134 | |||
| 135 | enum ServerDialogDownloadStatus | ||
| 136 | { | ||
| 137 | DOWNLOADING_ERROR, | ||
| 138 | DOWNLOADING_PREPARING, | ||
| 139 | DOWNLOADING_IDLE, | ||
| 140 | DOWNLOADING_IN_PROGRESS, | ||
| 141 | DOWNLOADING_COMPLETE, | ||
| 142 | DOWNLOADING_OVER | ||
| 143 | }; | ||
| 144 | |||
| 145 | /** Status of the current download. */ | ||
| 146 | ServerDialogDownloadStatus mDownloadStatus; | ||
| 147 | |||
| 148 | Net::Download *mDownload; | ||
| 149 | |||
| 150 | Mutex mMutex; | ||
| 151 | float mDownloadProgress; | ||
| 152 | |||
| 153 | ServerInfos mServers; | ||
| 124 | 154 | ServerInfo *mServerInfo; | |
| 125 | 155 | }; | |
| 126 | 156 |
src/gui/updatewindow.cpp
(52 / 209)
|   | |||
| 32 | 32 | #include "log.h" | |
| 33 | 33 | #include "main.h" | |
| 34 | 34 | ||
| 35 | #include "net/download.h" | ||
| 36 | |||
| 35 | 37 | #include "resources/resourcemanager.h" | |
| 36 | 38 | ||
| 37 | 39 | #include "utils/gettext.h" | |
| 38 | 40 | #include "utils/stringutils.h" | |
| 39 | 41 | ||
| 40 | 42 | #include <iostream> | |
| 41 | #include <SDL.h> | ||
| 42 | #include <SDL_thread.h> | ||
| 43 | #include <zlib.h> | ||
| 44 | 43 | ||
| 45 | #include <curl/curl.h> | ||
| 46 | |||
| 47 | 44 | /** | |
| 48 | * Calculates the Alder-32 checksum for the given file. | ||
| 49 | */ | ||
| 50 | static unsigned long fadler32(FILE *file) | ||
| 51 | { | ||
| 52 | // Obtain file size | ||
| 53 | fseek(file, 0, SEEK_END); | ||
| 54 | long fileSize = ftell(file); | ||
| 55 | rewind(file); | ||
| 56 | |||
| 57 | // Calculate Adler-32 checksum | ||
| 58 | char *buffer = (char*) malloc(fileSize); | ||
| 59 | const size_t read = fread(buffer, 1, fileSize, file); | ||
| 60 | unsigned long adler = adler32(0L, Z_NULL, 0); | ||
| 61 | adler = adler32(adler, (Bytef*) buffer, read); | ||
| 62 | free(buffer); | ||
| 63 | |||
| 64 | return adler; | ||
| 65 | } | ||
| 66 | |||
| 67 | /** | ||
| 68 | 45 | * Load the given file into a vector of strings. | |
| 69 | 46 | */ | |
| 70 | 47 | std::vector<std::string> loadTextFile(const std::string &fileName) | |
| … | … | ||
| 66 | 66 | UpdaterWindow::UpdaterWindow(const std::string &updateHost, | |
| 67 | 67 | const std::string &updatesDir): | |
| 68 | 68 | Window(_("Updating...")), | |
| 69 | mThread(NULL), | ||
| 70 | 69 | mDownloadStatus(UPDATE_NEWS), | |
| 71 | 70 | mUpdateHost(updateHost), | |
| 72 | 71 | mUpdatesDir(updatesDir), | |
| 73 | 72 | mCurrentFile("news.txt"), | |
| 73 | mDownloadProgress(0.0f), | ||
| 74 | 74 | mCurrentChecksum(0), | |
| 75 | 75 | mStoreInMemory(true), | |
| 76 | 76 | mDownloadComplete(true), | |
| 77 | 77 | mUserCancel(false), | |
| 78 | 78 | mDownloadedBytes(0), | |
| 79 | 79 | mMemoryBuffer(NULL), | |
| 80 | mCurlError(new char[CURL_ERROR_SIZE]), | ||
| 80 | mDownload(NULL), | ||
| 81 | 81 | mLineIndex(0) | |
| 82 | 82 | { | |
| 83 | mCurlError[0] = 0; | ||
| 84 | |||
| 85 | 83 | mBrowserBox = new BrowserBox; | |
| 86 | 84 | mScrollArea = new ScrollArea(mBrowserBox); | |
| 87 | 85 | mLabel = new Label(_("Connecting...")); | |
| … | … | ||
| 87 | 87 | mCancelButton = new Button(_("Cancel"), "cancel", this); | |
| 88 | 88 | mPlayButton = new Button(_("Play"), "play", this); | |
| 89 | 89 | ||
| 90 | mProgressBar->setSmoothProgress(false); | ||
| 90 | 91 | mBrowserBox->setOpaque(false); | |
| 91 | 92 | mPlayButton->setEnabled(false); | |
| 92 | 93 | ||
| … | … | ||
| 115 | 115 | ||
| 116 | 116 | UpdaterWindow::~UpdaterWindow() | |
| 117 | 117 | { | |
| 118 | if (mThread) | ||
| 119 | SDL_WaitThread(mThread, NULL); | ||
| 120 | |||
| 121 | 118 | free(mMemoryBuffer); | |
| 122 | |||
| 123 | // Remove possibly leftover temporary download | ||
| 124 | ::remove((mUpdatesDir + "/download.temp").c_str()); | ||
| 125 | |||
| 126 | delete[] mCurlError; | ||
| 127 | 119 | } | |
| 128 | 120 | ||
| 129 | 121 | void UpdaterWindow::setProgress(float p) | |
| 130 | 122 | { | |
| 131 | mProgressBar->setProgress(p); | ||
| 123 | // Do delayed progress bar update, since Guichan isn't thread-safe | ||
| 124 | MutexLocker lock(&mDownloadMutex); | ||
| 125 | mDownloadProgress = p; | ||
| 132 | 126 | } | |
| 133 | 127 | ||
| 134 | 128 | void UpdaterWindow::setLabel(const std::string &str) | |
| 135 | 129 | { | |
| 136 | 130 | // Do delayed label text update, since Guichan isn't thread-safe | |
| 137 | MutexLocker lock(&mLabelMutex); | ||
| 131 | MutexLocker lock(&mDownloadMutex); | ||
| 138 | 132 | mNewLabelCaption = str; | |
| 139 | 133 | } | |
| 140 | 134 | ||
| … | … | ||
| 148 | 148 | // Skip the updating process | |
| 149 | 149 | if (mDownloadStatus != UPDATE_COMPLETE) | |
| 150 | 150 | { | |
| 151 | mDownload->cancel(); | ||
| 151 | 152 | mDownloadStatus = UPDATE_ERROR; | |
| 152 | 153 | } | |
| 153 | 154 | } | |
| … | … | ||
| 187 | 187 | mScrollArea->setVerticalScrollAmount(0); | |
| 188 | 188 | } | |
| 189 | 189 | ||
| 190 | int UpdaterWindow::updateProgress(void *ptr, | ||
| 191 | double dt, double dn, double ut, double un) | ||
| 190 | int UpdaterWindow::updateProgress(void *ptr, DownloadStatus status, | ||
| 191 | size_t dt, size_t dn) | ||
| 192 | 192 | { | |
| 193 | float progress = dn / dt; | ||
| 194 | 193 | UpdaterWindow *uw = reinterpret_cast<UpdaterWindow *>(ptr); | |
| 195 | 194 | ||
| 195 | if (status == DOWNLOAD_STATUS_COMPLETE) | ||
| 196 | { | ||
| 197 | uw->mDownloadComplete = true; | ||
| 198 | } | ||
| 199 | else if (status == DOWNLOAD_STATUS_ERROR || | ||
| 200 | status == DOWNLOAD_STATUS_CANCELLED) | ||
| 201 | { | ||
| 202 | uw->mDownloadStatus = UPDATE_ERROR; | ||
| 203 | } | ||
| 204 | |||
| 205 | float progress = (float) dn / dt; | ||
| 206 | |||
| 196 | 207 | if (progress != progress) progress = 0.0f; // check for NaN | |
| 197 | 208 | if (progress < 0.0f) progress = 0.0f; // no idea how this could ever happen, but why not check for it anyway. | |
| 198 | 209 | if (progress > 1.0f) progress = 1.0f; | |
| … | … | ||
| 221 | 221 | return 0; | |
| 222 | 222 | } | |
| 223 | 223 | ||
| 224 | size_t UpdaterWindow::memoryWrite(void *ptr, size_t size, size_t nmemb, FILE *stream) | ||
| 224 | size_t UpdaterWindow::memoryWrite(void *ptr, size_t size, size_t nmemb, void *stream) | ||
| 225 | 225 | { | |
| 226 | 226 | UpdaterWindow *uw = reinterpret_cast<UpdaterWindow *>(stream); | |
| 227 | 227 | size_t totalMem = size * nmemb; | |
| … | … | ||
| 236 | 236 | return totalMem; | |
| 237 | 237 | } | |
| 238 | 238 | ||
| 239 | int UpdaterWindow::downloadThread(void *ptr) | ||
| 239 | void UpdaterWindow::download() | ||
| 240 | 240 | { | |
| 241 | int attempts = 0; | ||
| 242 | UpdaterWindow *uw = reinterpret_cast<UpdaterWindow *>(ptr); | ||
| 243 | CURL *curl; | ||
| 244 | CURLcode res; | ||
| 245 | std::string outFilename; | ||
| 246 | std::string url(uw->mUpdateHost + "/" + uw->mCurrentFile); | ||
| 241 | mDownload = new Net::Download(this, mUpdateHost + "/" + mCurrentFile, | ||
| 242 | updateProgress); | ||
| 247 | 243 | ||
| 248 | while (attempts < 3 && !uw->mDownloadComplete) | ||
| 244 | if (mStoreInMemory) | ||
| 249 | 245 | { | |
| 250 | FILE *outfile = NULL; | ||
| 251 | FILE *newfile = NULL; | ||
| 252 | uw->setLabel(uw->mCurrentFile + " (0%)"); | ||
| 253 | |||
| 254 | curl = curl_easy_init(); | ||
| 255 | |||
| 256 | if (curl) | ||
| 246 | mDownload->setWriteFunction(UpdaterWindow::memoryWrite); | ||
| 247 | } | ||
| 248 | else | ||
| 249 | { | ||
| 250 | if (mDownloadStatus == UPDATE_RESOURCES) | ||
| 257 | 251 | { | |
| 258 | logger->log("Downloading: %s", url.c_str()); | ||
| 259 | |||
| 260 | if (uw->mStoreInMemory) | ||
| 261 | { | ||
| 262 | uw->mDownloadedBytes = 0; | ||
| 263 | curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1); | ||
| 264 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, | ||
| 265 | UpdaterWindow::memoryWrite); | ||
| 266 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, ptr); | ||
| 267 | } | ||
| 268 | else | ||
| 269 | { | ||
| 270 | outFilename = uw->mUpdatesDir + "/download.temp"; | ||
| 271 | outfile = fopen(outFilename.c_str(), "w+b"); | ||
| 272 | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); | ||
| 273 | } | ||
| 274 | |||
| 275 | #ifdef PACKAGE_VERSION | ||
| 276 | curl_easy_setopt(curl, CURLOPT_USERAGENT, "TMW/" PACKAGE_VERSION); | ||
| 277 | #else | ||
| 278 | curl_easy_setopt(curl, CURLOPT_USERAGENT, "TMW"); | ||
| 279 | #endif | ||
| 280 | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, uw->mCurlError); | ||
| 281 | curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | ||
| 282 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); | ||
| 283 | curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, | ||
| 284 | UpdaterWindow::updateProgress); | ||
| 285 | curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, ptr); | ||
| 286 | curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1); | ||
| 287 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 15); | ||
| 288 | |||
| 289 | struct curl_slist *pHeaders = NULL; | ||
| 290 | if (uw->mDownloadStatus != UPDATE_RESOURCES) | ||
| 291 | { | ||
| 292 | // Make sure the resources2.txt and news.txt aren't cached, | ||
| 293 | // in order to always get the latest version. | ||
| 294 | pHeaders = curl_slist_append(pHeaders, "pragma: no-cache"); | ||
| 295 | pHeaders = | ||
| 296 | curl_slist_append(pHeaders, "Cache-Control: no-cache"); | ||
| 297 | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pHeaders); | ||
| 298 | } | ||
| 299 | |||
| 300 | if ((res = curl_easy_perform(curl)) != 0) | ||
| 301 | { | ||
| 302 | uw->mDownloadStatus = UPDATE_ERROR; | ||
| 303 | switch (res) | ||
| 304 | { | ||
| 305 | case CURLE_COULDNT_CONNECT: | ||
| 306 | default: | ||
| 307 | logger->log("curl error %d: %s host: %s", | ||
| 308 | res, uw->mCurlError, url.c_str()); | ||
| 309 | break; | ||
| 310 | } | ||
| 311 | |||
| 312 | if (!uw->mStoreInMemory) | ||
| 313 | { | ||
| 314 | fclose(outfile); | ||
| 315 | ::remove(outFilename.c_str()); | ||
| 316 | } | ||
| 317 | attempts++; | ||
| 318 | continue; | ||
| 319 | } | ||
| 320 | |||
| 321 | curl_easy_cleanup(curl); | ||
| 322 | |||
| 323 | if (uw->mDownloadStatus != UPDATE_RESOURCES) | ||
| 324 | { | ||
| 325 | curl_slist_free_all(pHeaders); | ||
| 326 | } | ||
| 327 | |||
| 328 | if (!uw->mStoreInMemory) | ||
| 329 | { | ||
| 330 | // Don't check resources2.txt checksum | ||
| 331 | if (uw->mDownloadStatus == UPDATE_RESOURCES) | ||
| 332 | { | ||
| 333 | unsigned long adler = fadler32(outfile); | ||
| 334 | |||
| 335 | if (uw->mCurrentChecksum != adler) | ||
| 336 | { | ||
| 337 | fclose(outfile); | ||
| 338 | |||
| 339 | // Remove the corrupted file | ||
| 340 | ::remove(outFilename.c_str()); | ||
| 341 | logger->log( | ||
| 342 | "Checksum for file %s failed: (%lx/%lx)", | ||
| 343 | uw->mCurrentFile.c_str(), | ||
| 344 | adler, uw->mCurrentChecksum); | ||
| 345 | attempts++; | ||
| 346 | continue; // Bail out here to avoid the renaming | ||
| 347 | } | ||
| 348 | } | ||
| 349 | fclose(outfile); | ||
| 350 | |||
| 351 | // Give the file the proper name | ||
| 352 | const std::string newName = | ||
| 353 | uw->mUpdatesDir + "/" + uw->mCurrentFile; | ||
| 354 | |||
| 355 | // Any existing file with this name is deleted first, otherwise | ||
| 356 | // the rename will fail on Windows. | ||
| 357 | ::remove(newName.c_str()); | ||
| 358 | ::rename(outFilename.c_str(), newName.c_str()); | ||
| 359 | |||
| 360 | // Check if we can open it and no errors were encountered | ||
| 361 | // during renaming | ||
| 362 | newfile = fopen(newName.c_str(), "rb"); | ||
| 363 | if (newfile) | ||
| 364 | { | ||
| 365 | fclose(newfile); | ||
| 366 | uw->mDownloadComplete = true; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | else | ||
| 370 | { | ||
| 371 | // It's stored in memory, we're done | ||
| 372 | uw->mDownloadComplete = true; | ||
| 373 | } | ||
| 252 | mDownload->setFile(mUpdatesDir + "/" + mCurrentFile, mCurrentChecksum); | ||
| 374 | 253 | } | |
| 375 | attempts++; | ||
| 254 | else | ||
| 255 | { | ||
| 256 | mDownload->setFile(mUpdatesDir + "/" + mCurrentFile); | ||
| 257 | } | ||
| 376 | 258 | } | |
| 377 | 259 | ||
| 378 | if (!uw->mDownloadComplete) { | ||
| 379 | uw->mDownloadStatus = UPDATE_ERROR; | ||
| 260 | if (mDownloadStatus != UPDATE_RESOURCES) | ||
| 261 | { | ||
| 262 | mDownload->noCache(); | ||
| 380 | 263 | } | |
| 381 | 264 | ||
| 382 | return 0; | ||
| 383 | } | ||
| 384 | |||
| 385 | void UpdaterWindow::download() | ||
| 386 | { | ||
| 265 | setLabel(mCurrentFile + " (0%)"); | ||
| 387 | 266 | mDownloadComplete = false; | |
| 388 | mThread = SDL_CreateThread(UpdaterWindow::downloadThread, this); | ||
| 389 | 267 | ||
| 390 | if (!mThread) | ||
| 391 | { | ||
| 392 | logger->log("Unable to create mThread"); | ||
| 393 | mDownloadStatus = UPDATE_ERROR; | ||
| 394 | } | ||
| 268 | // TODO: check return | ||
| 269 | mDownload->start(); | ||
| 395 | 270 | } | |
| 396 | 271 | ||
| 397 | 272 | void UpdaterWindow::logic() | |
| … | … | ||
| 276 | 276 | ||
| 277 | 277 | // Synchronize label caption when necessary | |
| 278 | 278 | { | |
| 279 | MutexLocker lock(&mLabelMutex); | ||
| 279 | MutexLocker lock(&mDownloadMutex); | ||
| 280 | 280 | ||
| 281 | 281 | if (mLabel->getCaption() != mNewLabelCaption) | |
| 282 | 282 | { | |
| 283 | 283 | mLabel->setCaption(mNewLabelCaption); | |
| 284 | 284 | mLabel->adjustSize(); | |
| 285 | 285 | } | |
| 286 | |||
| 287 | mProgressBar->setProgress(mDownloadProgress); | ||
| 286 | 288 | } | |
| 287 | 289 | ||
| 290 | std::string filename = mUpdatesDir + "/" + mCurrentFile; | ||
| 291 | |||
| 288 | 292 | switch (mDownloadStatus) | |
| 289 | 293 | { | |
| 290 | 294 | case UPDATE_ERROR: | |
| 291 | if (mThread) | ||
| 292 | { | ||
| 293 | if (mUserCancel) { | ||
| 294 | // Kill the thread, because user has canceled | ||
| 295 | SDL_KillThread(mThread); | ||
| 296 | // Set the flag to false again | ||
| 297 | mUserCancel = false; | ||
| 298 | } | ||
| 299 | else { | ||
| 300 | SDL_WaitThread(mThread, NULL); | ||
| 301 | } | ||
| 302 | mThread = NULL; | ||
| 303 | } | ||
| 304 | 295 | // TODO: Only send complete sentences to gettext | |
| 305 | 296 | mBrowserBox->addRow(""); | |
| 306 | 297 | mBrowserBox->addRow(_("##1 The update process is incomplete.")); | |
| … | … | ||
| 299 | 299 | mBrowserBox->addRow(_("##1 It is strongly recommended that")); | |
| 300 | 300 | // TRANSLATORS: Begins "It is strongly recommended that". | |
| 301 | 301 | mBrowserBox->addRow(_("##1 you try again later.")); | |
| 302 | mBrowserBox->addRow(mCurlError); | ||
| 302 | |||
| 303 | mBrowserBox->addRow(mDownload->getError()); | ||
| 303 | 304 | mScrollArea->setVerticalScrollAmount( | |
| 304 | 305 | mScrollArea->getVerticalMaxScroll()); | |
| 305 | 306 | mDownloadStatus = UPDATE_COMPLETE; | |
| … | … | ||
| 328 | 328 | case UPDATE_RESOURCES: | |
| 329 | 329 | if (mDownloadComplete) | |
| 330 | 330 | { | |
| 331 | if (mThread) | ||
| 332 | { | ||
| 333 | SDL_WaitThread(mThread, NULL); | ||
| 334 | mThread = NULL; | ||
| 335 | } | ||
| 336 | |||
| 337 | 331 | if (mLineIndex < mLines.size()) | |
| 338 | 332 | { | |
| 339 | 333 | std::stringstream line(mLines[mLineIndex]); |
src/gui/updatewindow.h
(15 / 20)
|   | |||
| 24 | 24 | ||
| 25 | 25 | #include "gui/widgets/window.h" | |
| 26 | 26 | ||
| 27 | #include "net/download.h" | ||
| 28 | |||
| 27 | 29 | #include "utils/mutex.h" | |
| 28 | 30 | ||
| 29 | 31 | #include <guichan/actionlistener.hpp> | |
| … | … | ||
| 38 | 38 | class ProgressBar; | |
| 39 | 39 | class ScrollArea; | |
| 40 | 40 | ||
| 41 | struct SDL_Thread; | ||
| 42 | |||
| 43 | 41 | /** | |
| 44 | 42 | * Update progress window GUI | |
| 45 | 43 | * | |
| … | … | ||
| 92 | 92 | void download(); | |
| 93 | 93 | ||
| 94 | 94 | /** | |
| 95 | * The thread function that download the files. | ||
| 95 | * A download callback for progress updates. | ||
| 96 | 96 | */ | |
| 97 | static int downloadThread(void *ptr); | ||
| 97 | static int updateProgress(void *ptr, DownloadStatus status, | ||
| 98 | size_t dt, size_t dn); | ||
| 98 | 99 | ||
| 99 | 100 | /** | |
| 100 | * A libcurl callback for progress updates. | ||
| 101 | */ | ||
| 102 | static int updateProgress(void *ptr, | ||
| 103 | double dt, double dn, double ut, double un); | ||
| 104 | |||
| 105 | /** | ||
| 106 | 101 | * A libcurl callback for writing to memory. | |
| 107 | 102 | */ | |
| 108 | 103 | static size_t memoryWrite(void *ptr, size_t size, size_t nmemb, | |
| 109 | FILE *stream); | ||
| 104 | void *stream); | ||
| 110 | 105 | ||
| 111 | enum DownloadStatus | ||
| 106 | enum UpdateDownloadStatus | ||
| 112 | 107 | { | |
| 113 | 108 | UPDATE_ERROR, | |
| 114 | 109 | UPDATE_IDLE, | |
| … | … | ||
| 113 | 113 | UPDATE_RESOURCES | |
| 114 | 114 | }; | |
| 115 | 115 | ||
| 116 | /** A thread that use libcurl to download updates. */ | ||
| 117 | SDL_Thread *mThread; | ||
| 118 | |||
| 119 | 116 | /** Status of the current download. */ | |
| 120 | DownloadStatus mDownloadStatus; | ||
| 117 | UpdateDownloadStatus mDownloadStatus; | ||
| 121 | 118 | ||
| 122 | 119 | /** Host where we get the updated files. */ | |
| 123 | 120 | std::string mUpdateHost; | |
| … | … | ||
| 128 | 128 | /** The new label caption to be set in the logic method. */ | |
| 129 | 129 | std::string mNewLabelCaption; | |
| 130 | 130 | ||
| 131 | /** The mutex used to guard access to mNewLabelCaption. */ | ||
| 132 | Mutex mLabelMutex; | ||
| 131 | /** The new progress value to be set in the logic method. */ | ||
| 132 | float mDownloadProgress; | ||
| 133 | 133 | ||
| 134 | /** The mutex used to guard access to mNewLabelCaption and mDownloadProgress. */ | ||
| 135 | Mutex mDownloadMutex; | ||
| 136 | |||
| 134 | 137 | /** The Adler32 checksum of the file currently downloading. */ | |
| 135 | 138 | unsigned long mCurrentChecksum; | |
| 136 | 139 | ||
| … | … | ||
| 152 | 152 | /** Buffer for files downloaded to memory. */ | |
| 153 | 153 | char *mMemoryBuffer; | |
| 154 | 154 | ||
| 155 | /** Buffer to handler human readable error provided by curl. */ | ||
| 156 | char *mCurlError; | ||
| 155 | /** Download handle. */ | ||
| 156 | Net::Download *mDownload; | ||
| 157 | 157 | ||
| 158 | 158 | /** List of files to download. */ | |
| 159 | 159 | std::vector<std::string> mLines; |
src/main.cpp
(7 / 6)
|   | |||
| 926 | 926 | loadUpdates(); | |
| 927 | 927 | } | |
| 928 | 928 | ||
| 929 | printf("State change: %d to %d\n", oldstate, state); | ||
| 929 | //printf("State change: %d to %d\n", oldstate, state); | ||
| 930 | 930 | ||
| 931 | 931 | oldstate = state; | |
| 932 | 932 | ||
| … | … | ||
| 945 | 945 | case STATE_CHOOSE_SERVER: | |
| 946 | 946 | logger->log("State: CHOOSE SERVER"); | |
| 947 | 947 | ||
| 948 | // Don't allow an alpha opacity | ||
| 949 | // lower than the default value | ||
| 950 | SkinLoader::instance()->setMinimumOpacity(0.8f); | ||
| 951 | |||
| 952 | 948 | // Allow changing this using a server choice dialog | |
| 953 | 949 | // We show the dialog box only if the command-line | |
| 954 | 950 | // options weren't set. | |
| 955 | 951 | if (options.serverName.empty() && options.serverPort == 0) { | |
| 956 | currentDialog = new ServerDialog(¤tServer); | ||
| 952 | // Don't allow an alpha opacity | ||
| 953 | // lower than the default value | ||
| 954 | SkinLoader::instance()->setMinimumOpacity(0.8f); | ||
| 955 | |||
| 956 | currentDialog = new ServerDialog(¤tServer, | ||
| 957 | homeDir); | ||
| 957 | 958 | } else { | |
| 958 | 959 | state = STATE_CONNECT_SERVER; | |
| 959 | 960 |
src/net/loginhandler.h
(1 / 2)
|   | |||
| 22 | 22 | #ifndef LOGINHANDLER_H | |
| 23 | 23 | #define LOGINHANDLER_H | |
| 24 | 24 | ||
| 25 | #include "logindata.h" | ||
| 26 | |||
| 25 | #include "net/logindata.h" | ||
| 27 | 26 | #include "net/serverinfo.h" | |
| 28 | 27 | #include "net/worldinfo.h" | |
| 29 | 28 |
src/net/net.cpp
(5 / 5)
|   | |||
| 125 | 125 | namespace Net | |
| 126 | 126 | { | |
| 127 | 127 | bool networkLoaded = false; | |
| 128 | } | ||
| 128 | } // namespace Net | ||
| 129 | 129 | ||
| 130 | 130 | void Net::connectToServer(const ServerInfo &server) | |
| 131 | 131 | { | |
| … | … | ||
| 134 | 134 | ||
| 135 | 135 | if (networkLoaded) | |
| 136 | 136 | { | |
| 137 | Net::getGeneralHandler()->reload(); | ||
| 137 | getGeneralHandler()->reload(); | ||
| 138 | 138 | } | |
| 139 | 139 | else | |
| 140 | 140 | { | |
| … | … | ||
| 145 | 145 | #endif | |
| 146 | 146 | } | |
| 147 | 147 | ||
| 148 | Net::getGeneralHandler()->load(); | ||
| 148 | getGeneralHandler()->load(); | ||
| 149 | 149 | ||
| 150 | 150 | networkLoaded = true; | |
| 151 | 151 | ||
| 152 | Net::getLoginHandler()->setServer(server); | ||
| 152 | getLoginHandler()->setServer(server); | ||
| 153 | 153 | ||
| 154 | Net::getLoginHandler()->connect(); | ||
| 154 | getLoginHandler()->connect(); | ||
| 155 | 155 | } |
src/net/net.h
(2 / 2)
|   | |||
| 19 | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 20 | 20 | */ | |
| 21 | 21 | ||
| 22 | #include "net/serverinfo.h" | ||
| 23 | |||
| 24 | 22 | #ifndef NET_H | |
| 25 | 23 | #define NET_H | |
| 24 | |||
| 25 | class ServerInfo; | ||
| 26 | 26 | ||
| 27 | 27 | namespace Net { | |
| 28 | 28 |
src/net/serverinfo.h
(3 / 0)
|   | |||
| 23 | 23 | #define SERVERINFO_H | |
| 24 | 24 | ||
| 25 | 25 | #include <string> | |
| 26 | #include <vector> | ||
| 26 | 27 | ||
| 27 | 28 | class ServerInfo | |
| 28 | 29 | { | |
| … | … | ||
| 49 | 49 | return (hostname != other.hostname || port != other.port); | |
| 50 | 50 | } | |
| 51 | 51 | }; | |
| 52 | |||
| 53 | typedef std::vector<ServerInfo> ServerInfos; | ||
| 52 | 54 | ||
| 53 | 55 | #endif // SERVERINFO_H |
tmw.cbp
(2 / 0)
|   | |||
| 459 | 459 | <Unit filename="src/net/adminhandler.h" /> | |
| 460 | 460 | <Unit filename="src/net/charhandler.h" /> | |
| 461 | 461 | <Unit filename="src/net/chathandler.h" /> | |
| 462 | <Unit filename="src/net/download.cpp" /> | ||
| 463 | <Unit filename="src/net/download.h" /> | ||
| 462 | 464 | <Unit filename="src/net/ea/adminhandler.cpp"> | |
| 463 | 465 | <Option target="eAthena" /> | |
| 464 | 466 | <Option target="UNIX eAthena" /> |

