[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[vidalia-svn] r3458: Shift some date, uptime and bandwidth formatting functions a (in vidalia/branches/marble/src: common vidalia/network)
Author: edmanm
Date: 2009-01-19 01:43:09 -0500 (Mon, 19 Jan 2009)
New Revision: 3458
Modified:
vidalia/branches/marble/src/common/stringutil.cpp
vidalia/branches/marble/src/common/stringutil.h
vidalia/branches/marble/src/vidalia/network/routerdescriptorview.cpp
vidalia/branches/marble/src/vidalia/network/routerdescriptorview.h
Log:
Shift some date, uptime and bandwidth formatting functions around so the other
kids can play with them too.
Modified: vidalia/branches/marble/src/common/stringutil.cpp
===================================================================
--- vidalia/branches/marble/src/common/stringutil.cpp 2009-01-19 01:31:47 UTC (rev 3457)
+++ vidalia/branches/marble/src/common/stringutil.cpp 2009-01-19 06:43:09 UTC (rev 3458)
@@ -14,6 +14,8 @@
** \brief Common string manipulation functions
*/
+#include <QCoreApplication>
+
#include "stringutil.h"
@@ -305,3 +307,49 @@
return true;
}
+/** Returns a human-readable description of the time elapsed given by
+ * <b>seconds</b>, broken down into days, hours, minutes and seconds. */
+QString
+string_format_uptime(quint64 seconds)
+{
+ QString uptime;
+ int secs = (seconds % 60);
+ int mins = (seconds / 60 % 60);
+ int hours = (seconds / 3600 % 24);
+ int days = (seconds / 86400);
+
+ if (days)
+ uptime += qApp->translate("stringutil.h", "%1 days ").arg(days);
+ if (hours)
+ uptime += qApp->translate("stringutil.h", "%1 hours ").arg(hours);
+ if (mins)
+ uptime += qApp->translate("stringutil.h", "%1 mins ").arg(mins);
+ if (secs)
+ uptime += qApp->translate("stringutil.h", "%1 secs").arg(secs);
+
+ return uptime;
+}
+
+/** Returns a string representation of <b>date</b> formatted according to
+ * "yyyy-MM-dd HH:mm:ss". */
+QString
+string_format_datetime(const QDateTime &date)
+{
+ return date.toString("yyyy-MM-dd HH:mm:ss");
+}
+
+/** Returns a string representation of <b>bytes</b> with the appropriate
+ * suffix of either "B/s", "KB/s", "MB/s" or "GB/s". */
+QString
+string_format_bandwidth(quint64 bytes)
+{
+ if (bytes < 1024)
+ return qApp->translate("stringutil.h", "%1 B/s").arg(bytes);
+ if (bytes < 1048576)
+ return qApp->translate("stringutil.h", "%1 KB/s").arg(bytes/1024.0, 0, 'f', 2);
+ if (bytes < 1073741824)
+ return qApp->translate("stringutil.h", "%1 MB/s").arg(bytes/1048576.0, 0, 'f', 2);
+
+ return qApp->translate("stringutil.h", "%1 GB/s").arg(bytes/1073741824.0, 0, 'f', 2);
+}
+
Modified: vidalia/branches/marble/src/common/stringutil.h
===================================================================
--- vidalia/branches/marble/src/common/stringutil.h 2009-01-19 01:31:47 UTC (rev 3457)
+++ vidalia/branches/marble/src/common/stringutil.h 2009-01-19 06:43:09 UTC (rev 3458)
@@ -19,6 +19,7 @@
#include <QStringList>
#include <QHash>
+#include <QDateTime>
/** Creates a QStringList from the array of C strings. */
@@ -73,5 +74,17 @@
* otherwise. */
bool string_is_hex(const QString &str);
+/** Returns a human-readable description of the time elapsed given by
+ * <b>seconds</b>, broken down into days, hours, minutes and seconds. */
+QString string_format_uptime(quint64 seconds);
+
+/** Returns a string representation of <b>date</b> formatted according to
+ * "yyyy-MM-dd HH:mm:ss". */
+QString string_format_datetime(const QDateTime &date);
+
+/** Returns a string representation of <b>bytes</b> with the appropriate
+ * (localized) suffix of either "B/s", "KB/s", "MB/s" or "GB/s". */
+QString string_format_bandwidth(quint64 bytes);
+
#endif
Modified: vidalia/branches/marble/src/vidalia/network/routerdescriptorview.cpp
===================================================================
--- vidalia/branches/marble/src/vidalia/network/routerdescriptorview.cpp 2009-01-19 01:31:47 UTC (rev 3457)
+++ vidalia/branches/marble/src/vidalia/network/routerdescriptorview.cpp 2009-01-19 06:43:09 UTC (rev 3458)
@@ -22,9 +22,10 @@
#include <QTextDocumentFragment>
#include <html.h>
#include <vidalia.h>
+#include <stringutil.h>
+
#include "routerdescriptorview.h"
-#define DATE_FORMAT "yyyy-MM-dd HH:mm:ss"
#define IMG_COPY ":/images/22x22/edit-copy.png"
@@ -66,13 +67,6 @@
vApp->clipboard()->setText(selectedText);
}
-/** Format the date the descriptor was published. */
-QString
-RouterDescriptorView::formatPublished(QDateTime date)
-{
- return date.toString(DATE_FORMAT) + " GMT";
-}
-
/** Adjusts the displayed uptime to include time since the router's descriptor
* was last published. */
quint64
@@ -86,38 +80,6 @@
return (uptime + (now.toTime_t() - published.toTime_t()));
}
-/** Format the uptime for this router in a readable format. */
-QString
-RouterDescriptorView::formatUptime(quint64 seconds)
-{
- QString uptime;
- int secs = (seconds % 60);
- int mins = (seconds / 60 % 60);
- int hours = (seconds / 3600 % 24);
- int days = (seconds / 86400);
-
- if (days) {
- uptime += tr("%1 days ").arg(days);
- }
- if (hours) {
- uptime += tr("%1 hours ").arg(hours);
- }
- if (mins) {
- uptime += tr("%1 mins ").arg(mins);
- }
- if (secs) {
- uptime += tr("%1 secs").arg(secs);
- }
- return uptime;
-}
-
-/** Format the bandwidth into KB/s. */
-QString
-RouterDescriptorView::formatBandwidth(quint64 bandwidth)
-{
- return QString::number(bandwidth/1024);
-}
-
/** Displays all router descriptors in the given list. */
void
RouterDescriptorView::display(QList<RouterDescriptor> rdlist)
@@ -146,18 +108,18 @@
/* If the router is online, then show the uptime and bandwidth stats. */
if (!rd.offline()) {
html.append(trow(tcol(b(tr("Bandwidth:"))) +
- tcol(formatBandwidth(rd.observedBandwidth()) + " KB/s")));
+ tcol(string_format_bandwidth(rd.observedBandwidth()))));
html.append(trow(tcol(b(tr("Uptime:"))) +
- tcol(formatUptime(
+ tcol(string_format_uptime(
adjustUptime(rd.uptime(), rd.published())))));
}
-
+
/* Date the router was published */
html.append(trow(tcol(b(tr("Last Updated:"))) +
- tcol(formatPublished(rd.published()))));
-
+ tcol(string_format_datetime(rd.published()) + " GMT")));
+
html.append("</table>");
-
+
/* If there are multiple descriptors, and this isn't is the last one
* then separate them with a short horizontal line. */
if (r+1 != rdlist.size()) {
Modified: vidalia/branches/marble/src/vidalia/network/routerdescriptorview.h
===================================================================
--- vidalia/branches/marble/src/vidalia/network/routerdescriptorview.h 2009-01-19 01:31:47 UTC (rev 3457)
+++ vidalia/branches/marble/src/vidalia/network/routerdescriptorview.h 2009-01-19 06:43:09 UTC (rev 3458)
@@ -50,12 +50,6 @@
/** Adjusts the displayed uptime to include time since the
* router's descriptor was last published. */
quint64 adjustUptime(quint64 uptime, QDateTime published);
- /** Formats the descriptor's published date. */
- QString formatPublished(QDateTime date);
- /** Formats the router's uptime. */
- QString formatUptime(quint64 seconds);
- /** Formats the observed bandwidth into KB/s. */
- QString formatBandwidth(quint64 bandwidth);
};
#endif