34 #include <QApplication>
35 #include <QTranslator>
39 #include <QTemporaryFile>
57 buf.open (QIODevice::ReadWrite);
58 pix.save (&buf,
"PNG", 100);
59 return QString (
"data:image/png;base64,%1")
60 .arg (QString (buf.buffer ().toBase64 ()));
65 QString
string = QObject::tr (
"Too long to show");
67 p.
Additional_ [
"UserVisibleName"].canConvert<QString> ())
68 string = p.
Additional_ [
"UserVisibleName"].toString ();
69 else if (p.
Entity_.canConvert<QByteArray> ())
71 QByteArray entity = p.
Entity_.toByteArray ();
72 if (entity.size () < 100)
73 string = QTextCodec::codecForName (
"UTF-8")->toUnicode (entity);
75 else if (p.
Entity_.canConvert<QUrl> ())
77 string = p.
Entity_.toUrl ().toString ();
78 if (
string.size () > 100)
79 string =
string.left (97) +
"...";
82 string = QObject::tr (
"Binary entity");
84 if (!p.
Mime_.isEmpty ())
85 string += QObject::tr (
"<br /><br />of type <code>%1</code>").arg (p.
Mime_);
87 if (!p.
Additional_ [
"SourceURL"].toUrl ().isEmpty ())
89 QString urlStr = p.
Additional_ [
"SourceURL"].toUrl ().toString ();
90 if (urlStr.size () > 63)
91 urlStr = urlStr.left (60) +
"...";
92 string += QObject::tr (
"<br />from %1")
102 long double size = sourcesize;
122 return QString::number (size,
'f', 1) + QObject::tr (
" b");
124 return QString::number (size,
'f', 1) + QObject::tr (
" KiB");
126 return QString::number (size,
'f', 1) + QObject::tr (
" MiB");
128 return QString::number (size,
'f', 1) + QObject::tr (
" GiB");
136 int d = time / 86400;
140 result += QObject::tr (
"%n day(s), ",
"", d);
141 result += QTime (0, 0, 0).addSecs (time).toString ();
146 const QString& prefix,
147 const QString& appName)
150 QString filename = prefix;
151 filename.append (
"_");
152 if (!baseName.isEmpty ())
153 filename.append (baseName).append (
"_");
154 filename.append (localeName);
156 QTranslator *transl =
new QTranslator;
158 if (transl->load (filename,
":/") ||
159 transl->load (filename,
160 QCoreApplication::applicationDirPath () +
"/translations"))
161 #elif defined (Q_OS_MAC)
162 if (transl->load (filename,
":/") ||
163 transl->load (filename,
164 QCoreApplication::applicationDirPath () +
"/../Resources/translations"))
165 #elif defined (INSTALL_PREFIX)
166 if (transl->load (filename,
":/") ||
167 transl->load (filename,
168 QString (INSTALL_PREFIX
"/share/%1/translations").arg (appName)))
170 if (transl->load (filename,
":/") ||
171 transl->load (filename,
172 QString (
"/usr/local/share/%1/translations").arg (appName)) ||
173 transl->load (filename,
174 QString (
"/usr/share/%1/translations").arg (appName)))
177 qApp->installTranslator (transl);
182 qWarning () << Q_FUNC_INFO
183 <<
"could not load translation file for locale"
191 QSettings settings (QCoreApplication::organizationName (),
192 QCoreApplication::applicationName ());
193 QString localeName = settings.value (
"Language",
"system").toString ();
195 if (localeName ==
"system")
197 localeName = QString (::getenv (
"LANG")).left (5);
198 if (localeName.isEmpty () || localeName.size () != 5)
199 localeName = QLocale::system ().name ();
200 localeName = localeName.left (5);
203 if (localeName.size () == 2)
205 QLocale::Language lang = QLocale (localeName).language ();
206 QList<QLocale::Country> cs = QLocale::countriesForLanguage (lang);
210 localeName = QLocale (lang, cs.at (0)).name ();
218 #if QT_VERSION >= 0x040800
219 if (locale.language () == QLocale::AnyLanguage)
223 QString locStr = locale.name ();
224 locStr.replace (
'_',
'-');
235 QString path = opath;
237 QDir home = QDir::home ();
238 path.prepend (
".leechcraft/");
240 if (!home.exists (path) &&
242 throw std::runtime_error (qPrintable (QObject::tr (
"Could not create %1")
243 .arg (QDir::toNativeSeparators (home.filePath (path)))));
248 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
249 .arg (QDir::toNativeSeparators (home.filePath (path)))));
254 QString path = opath;
255 QDir home = QDir::home ();
256 path.prepend (
".leechcraft/");
258 if (!home.exists (path))
259 throw std::runtime_error (qPrintable (QString (
"The specified path doesn't exist: %1")
260 .arg (QDir::toNativeSeparators (home.filePath (path)))));
265 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
266 .arg (QDir::toNativeSeparators (home.filePath (path)))));
271 QTemporaryFile file (QDir::tempPath () +
"/" + pattern);
273 QString name = file.fileName ();
280 const QString& location,
281 LeechCraft::TaskParameters tp,
293 const QString& text,
Priority priority)
298 "x-leechcraft/notification");
307 e.
Additional_ [
"org.LC.AdvNotifications.SenderID"] =
event.Additional_ [
"org.LC.AdvNotifications.SenderID"];
308 e.
Additional_ [
"org.LC.AdvNotifications.EventID"] =
event.Additional_ [
"org.LC.AdvNotifications.EventID"];
309 e.
Additional_ [
"org.LC.AdvNotifications.EventCategory"] =
"org.LC.AdvNotifications.Cancel";
316 e.
Additional_ [
"org.LC.AdvNotifications.SenderID"] = senderId;
317 e.
Additional_ [
"org.LC.AdvNotifications.EventID"] = eventId;
318 e.
Additional_ [
"org.LC.AdvNotifications.EventCategory"] =
"org.LC.AdvNotifications.Cancel";
324 QAction *senderAct = qobject_cast<QAction*> (sender);
329 QDebug d (&debugString);
330 d <<
"sender is not a QAction*"
333 throw std::runtime_error (qPrintable (debugString));
337 property (
"SelectedRows").value<QList<QModelIndex>> ();
342 QAction *result =
new QAction (parent);
343 result->setSeparator (
true);
353 "x-leechcraft/data-persistent-load");
355 e.
Additional_ [
"Values"] = QVariant::fromValue<QVariantList*> (&values);
357 QMetaObject::invokeMethod (
object,
361 Q_ARG (QObject**, 0));
367 const QString& text, QFont font,
const QPen& pen,
const QBrush& brush)
369 const auto& iconSize = px.size ();
371 const auto fontHeight = px.height () * 0.45;
372 font.setPixelSize (std::max (6., fontHeight));
374 const QFontMetrics fm (font);
375 const auto width = fm.width (text) + 2. * px.width () / 10.;
376 const auto height = fm.height () + 2. * px.height () / 10.;
377 const bool tooSmall = width > iconSize.width ();
379 const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
385 p.setRenderHint (QPainter::Antialiasing);
386 p.setRenderHint (QPainter::TextAntialiasing);
387 p.setRenderHint (QPainter::HighQualityAntialiasing);
388 p.drawRoundedRect (textRect, 4, 4);
389 p.drawText (textRect,
391 tooSmall ?
"#" : text);
UTIL_API QVariantList GetPersistentData(const QList< QVariant > &keys, QObject *object)
static UTIL_API Util::IDPool< qint64 > IDPool_
UTIL_API QDir GetUserDir(const QString &path)
Returns the path relative to user directory.
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
UTIL_API QString GetTemporaryName(const QString &pattern=QString("lc_temp.XXXXXX"))
Returns a temporary filename.
UTIL_API QString MakePrettySize(qint64)
Makes a formatted size from number.
TaskParameters Parameters_
Parameters of this task.
UTIL_API QDir CreateIfNotExists(const QString &path)
Creates a path if it doesn't exist.
UTIL_API QString GetLanguage()
Returns the current language name.
UTIL_API QString GetInternetLocaleName(const QLocale &)
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
UTIL_API QString MakeTimeFromLong(ulong)
Makes a formatted time from number.
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
QMap< QString, QVariant > Additional_
Additional parameters.
UTIL_API Entity MakeANCancel(const Entity &event)
Makes an event for canceling another Advanced Notifications event.
QString Mime_
MIME type of the entity.
UTIL_API Entity MakeEntity(const QVariant &entity, const QString &location, LeechCraft::TaskParameters tp, const QString &mime=QString())
An utility function to make a Entity.
QVariant Entity_
The entity that this object represents.
UTIL_API Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Describes parameters of an entity.
Q_DECLARE_METATYPE(LeechCraft::IHookProxy_ptr)
QString Location_
Source or destination.