7 #include <QApplication>
10 #include <QImageWriter>
11 #include <QMessageBox>
14 #include <QSystemTrayIcon>
15 #include <QTemporaryFile>
26 #include <ApplicationServices/ApplicationServices.h>
37 programName(programName),
44 if(trayicon && trayicon->supportsMessages())
49 interface = new QDBusInterface("org.freedesktop.Notifications",
50 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
51 if(interface->isValid())
64 OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, CFSTR(
"growlTicket"), kLSRolesAll, 0, &cfurl);
65 if (status != kLSApplicationNotFoundErr) {
66 CFBundleRef bundle = CFBundleCreate(0, cfurl);
67 if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR(
"com.Growl.GrowlHelperApp"), kCFCompareCaseInsensitive | kCFCompareBackwards) == kCFCompareEqualTo) {
68 if (CFStringHasSuffix(CFURLGetString(cfurl), CFSTR(
"/Growl.app/")))
90 class FreedesktopImage
94 FreedesktopImage(
const QImage &img);
96 static int metaType();
99 static QVariant toVariant(
const QImage &img);
102 int width, height, stride;
108 friend QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i);
109 friend const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i);
112 Q_DECLARE_METATYPE(FreedesktopImage);
115 const int CHANNELS = 4;
116 const int BYTES_PER_PIXEL = 4;
117 const int BITS_PER_SAMPLE = 8;
119 FreedesktopImage::FreedesktopImage(
const QImage &img):
121 height(img.height()),
122 stride(img.width() * BYTES_PER_PIXEL),
125 bitsPerSample(BITS_PER_SAMPLE)
128 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
129 const uint32_t *
data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
131 unsigned int num_pixels = width * height;
132 image.resize(num_pixels * BYTES_PER_PIXEL);
134 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
136 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16;
137 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;
138 image[ptr*BYTES_PER_PIXEL+2] = data[ptr];
139 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24;
143 QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i)
146 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
151 const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i)
154 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
159 int FreedesktopImage::metaType()
161 return qDBusRegisterMetaType<FreedesktopImage>();
164 QVariant FreedesktopImage::toVariant(
const QImage &img)
166 FreedesktopImage fimg(img);
167 return QVariant(FreedesktopImage::metaType(), &fimg);
170 void Notificator::notifyDBus(Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
174 QList<QVariant> args;
183 args.append(QString());
193 args.append(actions);
202 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
205 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
206 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
207 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
210 tmpicon = QApplication::style()->standardIcon(sicon);
216 hints[
"icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
220 args.append(millisTimeout);
223 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
230 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
233 case Information: sicon = QSystemTrayIcon::Information;
break;
234 case Warning: sicon = QSystemTrayIcon::Warning;
break;
235 case Critical: sicon = QSystemTrayIcon::Critical;
break;
237 trayIcon->showMessage(title, text, sicon, millisTimeout);
242 void Notificator::notifyGrowl(Class cls,
const QString &title,
const QString &text,
const QIcon &icon)
244 const QString script(
245 "tell application \"%5\"\n"
246 " set the allNotificationsList to {\"Notification\"}\n"
247 " set the enabledNotificationsList to {\"Notification\"}\n"
248 " register as application \"%1\" all notifications allNotificationsList default notifications enabledNotificationsList\n"
249 " notify with name \"Notification\" title \"%2\" description \"%3\" application name \"%1\"%4\n"
253 QString notificationApp(QApplication::applicationName());
254 if (notificationApp.isEmpty())
255 notificationApp =
"Application";
257 QPixmap notificationIconPixmap;
259 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
262 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
263 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
264 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
266 notificationIconPixmap = QApplication::style()->standardPixmap(sicon);
269 QSize size = icon.actualSize(QSize(48, 48));
270 notificationIconPixmap = icon.pixmap(size);
273 QString notificationIcon;
274 QTemporaryFile notificationIconFile;
275 if (!notificationIconPixmap.isNull() && notificationIconFile.open()) {
276 QImageWriter writer(¬ificationIconFile,
"PNG");
277 if (writer.write(notificationIconPixmap.toImage()))
278 notificationIcon = QString(
" image from location \"file://%1\"").arg(notificationIconFile.fileName());
281 QString quotedTitle(title), quotedText(text);
282 quotedTitle.replace(
"\\",
"\\\\").replace(
"\"",
"\\");
283 quotedText.replace(
"\\",
"\\\\").replace(
"\"",
"\\");
288 void Notificator::notifyMacUserNotificationCenter(Class cls,
const QString &title,
const QString &text,
const QIcon &icon) {
301 notifyDBus(cls, title, text, icon, millisTimeout);
309 notifyMacUserNotificationCenter(cls, title, text, icon);
313 notifyGrowl(cls, title, text, icon);
320 QMessageBox::critical(
parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
bool hasUserNotificationCenterSupport(void)
check if OS can handle UserNotifications
Use DBus org.freedesktop.Notifications.
Notificator(const QString &programName, QSystemTrayIcon *trayIcon, QWidget *parent)
Create a new notificator.
Notify user of potential problem.
const CBigNum operator<<(const CBigNum &a, unsigned int shift)
Use the 10.8+ User Notification Center (Mac only)
const CBigNum operator>>(const CBigNum &a, unsigned int shift)
void notify(Class cls, const QString &title, const QString &text, const QIcon &icon=QIcon(), int millisTimeout=10000)
Show notification message.
static MacNotificationHandler * instance()
void sendAppleScript(const QString &script)
executes AppleScript
QSystemTrayIcon * trayIcon
const int FREEDESKTOP_NOTIFICATION_ICON_SIZE
Use the Growl 1.2 notification system (Mac only)
Use QSystemTray::showMessage.
void notifySystray(Class cls, const QString &title, const QString &text, const QIcon &icon, int millisTimeout)
static const CCheckpointData data
void showNotification(const QString &title, const QString &text)
shows a 10.8+ UserNotification in the UserNotificationCenter
Use the Growl 1.3 notification system (Mac only)