예제 #1
0
	void CurrenciesManager::gotRateReply ()
	{
		auto reply = qobject_cast<QNetworkReply*> (sender ());
		reply->deleteLater ();

		const auto& data = reply->readAll ();

		QDomDocument doc;
		if (!doc.setContent (data))
		{
			qWarning () << Q_FUNC_INFO
					<< "unable to parse"
					<< data;
			return;
		}

		const auto& now = QDateTime::currentDateTime ();

		bool changed = false;
		auto rateElem = doc.documentElement ()
				.firstChildElement ("results")
				.firstChildElement ("rate");
		while (!rateElem.isNull ())
		{
			std::shared_ptr<void> guard (nullptr,
					[&rateElem] (void*) { rateElem = rateElem.nextSiblingElement ("rate"); });

			const auto& toValue = rateElem.attribute ("id").mid (3);
			if (toValue.size () != 3)
			{
				qWarning () << "incorrect `to` value"
						<< toValue;
				continue;
			}

			const auto newRate = rateElem.firstChildElement ("Rate").text ().toDouble ();
			if (std::fabs (newRate - RatesFromUSD_ [toValue]) > std::numeric_limits<double>::epsilon ())
			{
				RatesFromUSD_ [toValue] = newRate;
				changed = true;
			}

			Rate rate { 0, toValue, now, newRate };
			Core::Instance ().GetStorage ()->AddRate (rate);
		}

		LastFetch_ = QDateTime::currentDateTime ();

		QSettings settings (QCoreApplication::organizationName (),
			QCoreApplication::applicationName () + "_Poleemery");
		settings.beginGroup ("Currencies");
		settings.setValue ("LastFetch", LastFetch_);
		if (changed)
		{
			emit currenciesUpdated ();
			for (auto i = RatesFromUSD_.constBegin (); i != RatesFromUSD_.constEnd (); ++i)
				settings.setValue (i.key (), *i);
		}
		settings.endGroup ();
	}
	void OperationsManager::Load ()
	{
		QList<EntryBase_ptr> entries;
		for (const auto& entry : Storage_->GetReceiptEntries ())
			entries << std::make_shared<ReceiptEntry> (entry);
		for (const auto& entry : Storage_->GetExpenseEntries ())
		{
			entries << std::make_shared<ExpenseEntry> (entry);
			for (const auto& cat : entry.Categories_)
				KnownCategories_ << cat;
		}
		for (auto entry : entries)
		{
			auto& date = entry->Date_;
			const auto& time = date.time ();
			date.setTime ({ time.hour (), time.minute () });
		}
		Model_->AddEntries (entries);

		connect (Core::Instance ().GetCurrenciesManager (),
				SIGNAL (currenciesUpdated ()),
				Model_,
				SLOT (recalcSums ()));
	}