6 #include <QApplication>
9 #include <QSystemTrayIcon>
10 #include <QMessageBox>
11 #include <QTemporaryFile>
12 #include <QImageWriter>
20 #include <ApplicationServices/ApplicationServices.h>
30 programName(programName),
37 if(trayicon && trayicon->supportsMessages())
42 interface = new QDBusInterface("org.freedesktop.Notifications",
43 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
44 if(interface->isValid())
57 OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, CFSTR(
"growlTicket"), kLSRolesAll, 0, &cfurl);
58 if (status != kLSApplicationNotFoundErr) {
59 CFBundleRef bundle = CFBundleCreate(0, cfurl);
60 if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR(
"com.Growl.GrowlHelperApp"), kCFCompareCaseInsensitive | kCFCompareBackwards) == kCFCompareEqualTo) {
61 if (CFStringHasSuffix(CFURLGetString(cfurl), CFSTR(
"/Growl.app/")))
83 class FreedesktopImage
87 FreedesktopImage(
const QImage &img);
89 static int metaType();
92 static QVariant toVariant(
const QImage &img);
95 int width, height, stride;
101 friend QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i);
102 friend const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i);
105 Q_DECLARE_METATYPE(FreedesktopImage);
108 const int CHANNELS = 4;
109 const int BYTES_PER_PIXEL = 4;
110 const int BITS_PER_SAMPLE = 8;
112 FreedesktopImage::FreedesktopImage(
const QImage &img):
114 height(img.height()),
115 stride(img.width() * BYTES_PER_PIXEL),
118 bitsPerSample(BITS_PER_SAMPLE)
121 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
124 unsigned int num_pixels = width * height;
125 image.resize(num_pixels * BYTES_PER_PIXEL);
127 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
129 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16;
130 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;
131 image[ptr*BYTES_PER_PIXEL+2] = data[ptr];
132 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24;
136 QDBusArgument &
operator<<(QDBusArgument &a,
const FreedesktopImage &i)
139 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
144 const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i)
147 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
152 int FreedesktopImage::metaType()
154 return qDBusRegisterMetaType<FreedesktopImage>();
157 QVariant FreedesktopImage::toVariant(
const QImage &img)
159 FreedesktopImage fimg(img);
160 return QVariant(FreedesktopImage::metaType(), &fimg);
163 void Notificator::notifyDBus(Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
167 QList<QVariant> args;
176 args.append(QString());
186 args.append(actions);
195 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
198 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
199 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
200 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
203 tmpicon = QApplication::style()->standardIcon(sicon);
213 args.append(millisTimeout);
216 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
223 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
226 case Information: sicon = QSystemTrayIcon::Information;
break;
227 case Warning: sicon = QSystemTrayIcon::Warning;
break;
228 case Critical: sicon = QSystemTrayIcon::Critical;
break;
230 trayIcon->showMessage(title, text, sicon, millisTimeout);
235 void Notificator::notifyGrowl(Class cls,
const QString &title,
const QString &text,
const QIcon &icon)
237 const QString script(
238 "tell application \"%5\"\n"
239 " set the allNotificationsList to {\"Notification\"}\n"
240 " set the enabledNotificationsList to {\"Notification\"}\n"
241 " register as application \"%1\" all notifications allNotificationsList default notifications enabledNotificationsList\n"
242 " notify with name \"Notification\" title \"%2\" description \"%3\" application name \"%1\"%4\n"
246 QString notificationApp(QApplication::applicationName());
247 if (notificationApp.isEmpty())
248 notificationApp =
"Application";
250 QPixmap notificationIconPixmap;
252 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
255 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
256 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
257 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
259 notificationIconPixmap = QApplication::style()->standardPixmap(sicon);
262 QSize size = icon.actualSize(QSize(48, 48));
263 notificationIconPixmap = icon.pixmap(size);
266 QString notificationIcon;
267 QTemporaryFile notificationIconFile;
268 if (!notificationIconPixmap.isNull() && notificationIconFile.open()) {
269 QImageWriter writer(¬ificationIconFile,
"PNG");
270 if (writer.write(notificationIconPixmap.toImage()))
271 notificationIcon = QString(
" image from location \"file://%1\"").arg(notificationIconFile.fileName());
274 QString quotedTitle(title), quotedText(text);
275 quotedTitle.replace(
"\\",
"\\\\").replace(
"\"",
"\\");
276 quotedText.replace(
"\\",
"\\\\").replace(
"\"",
"\\");
281 void Notificator::notifyMacUserNotificationCenter(Class cls,
const QString &title,
const QString &text,
const QIcon &icon) {
294 notifyDBus(cls, title, text, icon, millisTimeout);
302 notifyMacUserNotificationCenter(cls, title, text, icon);
306 notifyGrowl(cls, title, text, icon);
313 QMessageBox::critical(
parent, title, text, QMessageBox::Ok, QMessageBox::Ok);
bool hasUserNotificationCenterSupport(void)
check if OS can handle UserNotifications
Use DBus org.freedesktop.Notifications.
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
Notificator(const QString &programName=QString(), QSystemTrayIcon *trayIcon=0, QWidget *parent=0)
Create a new notificator.
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)
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)