7 #include <QApplication> 10 #include <QImageWriter> 11 #include <QMessageBox> 14 #include <QSystemTrayIcon> 15 #include <QTemporaryFile> 26 #include <ApplicationServices/ApplicationServices.h> 33 const int FREEDESKTOP_NOTIFICATION_ICON_SIZE = 128;
39 programName(_programName),
46 if(_trayIcon && _trayIcon->supportsMessages())
51 interface = new QDBusInterface("org.freedesktop.Notifications",
52 "/org/freedesktop/Notifications", "org.freedesktop.Notifications");
53 if(interface->isValid())
66 OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator, CFSTR(
"growlTicket"), kLSRolesAll, 0, &cfurl);
67 if (status != kLSApplicationNotFoundErr) {
68 CFBundleRef bundle = CFBundleCreate(0, cfurl);
69 if (CFStringCompare(CFBundleGetIdentifier(bundle), CFSTR(
"com.Growl.GrowlHelperApp"), kCFCompareCaseInsensitive | kCFCompareBackwards) == kCFCompareEqualTo) {
70 if (CFStringHasSuffix(CFURLGetString(cfurl), CFSTR(
"/Growl.app/")))
92 class FreedesktopImage
96 FreedesktopImage(
const QImage &img);
98 static int metaType();
101 static QVariant toVariant(
const QImage &img);
104 int width, height, stride;
110 friend QDBusArgument &
operator<<(QDBusArgument &
a,
const FreedesktopImage &i);
111 friend const QDBusArgument &
operator>>(
const QDBusArgument &a, FreedesktopImage &i);
114 Q_DECLARE_METATYPE(FreedesktopImage);
117 const int CHANNELS = 4;
118 const int BYTES_PER_PIXEL = 4;
119 const int BITS_PER_SAMPLE = 8;
121 FreedesktopImage::FreedesktopImage(
const QImage &img):
123 height(img.height()),
124 stride(img.width() * BYTES_PER_PIXEL),
127 bitsPerSample(BITS_PER_SAMPLE)
130 QImage tmp = img.convertToFormat(QImage::Format_ARGB32);
131 const uint32_t *
data =
reinterpret_cast<const uint32_t*
>(tmp.bits());
133 unsigned int num_pixels = width * height;
134 image.resize(num_pixels * BYTES_PER_PIXEL);
136 for(
unsigned int ptr = 0; ptr < num_pixels; ++ptr)
138 image[ptr*BYTES_PER_PIXEL+0] = data[ptr] >> 16;
139 image[ptr*BYTES_PER_PIXEL+1] = data[ptr] >> 8;
140 image[ptr*BYTES_PER_PIXEL+2] = data[ptr];
141 image[ptr*BYTES_PER_PIXEL+3] = data[ptr] >> 24;
145 QDBusArgument &
operator<<(QDBusArgument &
a,
const FreedesktopImage &i)
148 a << i.width << i.height << i.stride << i.hasAlpha << i.bitsPerSample << i.channels << i.image;
153 const QDBusArgument &
operator>>(
const QDBusArgument &
a, FreedesktopImage &i)
156 a >> i.width >> i.height >> i.stride >> i.hasAlpha >> i.bitsPerSample >> i.channels >> i.image;
161 int FreedesktopImage::metaType()
163 return qDBusRegisterMetaType<FreedesktopImage>();
166 QVariant FreedesktopImage::toVariant(
const QImage &img)
168 FreedesktopImage fimg(img);
169 return QVariant(FreedesktopImage::metaType(), &fimg);
172 void Notificator::notifyDBus(
Class cls,
const QString &title,
const QString &text,
const QIcon &icon,
int millisTimeout)
176 QList<QVariant> args;
185 args.append(QString());
195 args.append(actions);
204 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
207 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
208 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
209 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
212 tmpicon = QApplication::style()->standardIcon(sicon);
218 hints[
"icon_data"] = FreedesktopImage::toVariant(tmpicon.pixmap(FREEDESKTOP_NOTIFICATION_ICON_SIZE).toImage());
222 args.append(millisTimeout);
225 interface->callWithArgumentList(QDBus::NoBlock,
"Notify", args);
232 QSystemTrayIcon::MessageIcon sicon = QSystemTrayIcon::NoIcon;
235 case Information: sicon = QSystemTrayIcon::Information;
break;
236 case Warning: sicon = QSystemTrayIcon::Warning;
break;
237 case Critical: sicon = QSystemTrayIcon::Critical;
break;
239 trayIcon->showMessage(title, text, sicon, millisTimeout);
244 void Notificator::notifyGrowl(
Class cls,
const QString &title,
const QString &text,
const QIcon &icon)
246 const QString script(
247 "tell application \"%5\"\n" 248 " set the allNotificationsList to {\"Notification\"}\n" 249 " set the enabledNotificationsList to {\"Notification\"}\n" 250 " register as application \"%1\" all notifications allNotificationsList default notifications enabledNotificationsList\n" 251 " notify with name \"Notification\" title \"%2\" description \"%3\" application name \"%1\"%4\n" 255 QString notificationApp(QApplication::applicationName());
256 if (notificationApp.isEmpty())
257 notificationApp =
"Application";
259 QPixmap notificationIconPixmap;
261 QStyle::StandardPixmap sicon = QStyle::SP_MessageBoxQuestion;
264 case Information: sicon = QStyle::SP_MessageBoxInformation;
break;
265 case Warning: sicon = QStyle::SP_MessageBoxWarning;
break;
266 case Critical: sicon = QStyle::SP_MessageBoxCritical;
break;
268 notificationIconPixmap = QApplication::style()->standardPixmap(sicon);
271 QSize
size = icon.actualSize(QSize(48, 48));
272 notificationIconPixmap = icon.pixmap(size);
275 QString notificationIcon;
276 QTemporaryFile notificationIconFile;
277 if (!notificationIconPixmap.isNull() && notificationIconFile.open()) {
278 QImageWriter writer(¬ificationIconFile,
"PNG");
279 if (writer.write(notificationIconPixmap.toImage()))
280 notificationIcon = QString(
" image from location \"file://%1\"").arg(notificationIconFile.fileName());
283 QString quotedTitle(title), quotedText(text);
284 quotedTitle.replace(
"\\",
"\\\\").replace(
"\"",
"\\");
285 quotedText.replace(
"\\",
"\\\\").replace(
"\"",
"\\");
290 void Notificator::notifyMacUserNotificationCenter(
Class cls,
const QString &title,
const QString &text,
const QIcon &icon) {
303 notifyDBus(cls, title, text, icon, millisTimeout);
311 notifyMacUserNotificationCenter(cls, title, text, icon);
315 notifyGrowl(cls, title, text, icon);
322 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.
Use the 10.8+ User Notification Center (Mac only)
void notify(Class cls, const QString &title, const QString &text, const QIcon &icon=QIcon(), int millisTimeout=10000)
Show notification message.
static MacNotificationHandler * instance()
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
void sendAppleScript(const QString &script)
executes AppleScript
QSystemTrayIcon * trayIcon
uint8_t const size_t const size
Use the Growl 1.2 notification system (Mac only)
std::istream & operator>>(std::istream &in, Integer &a)
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)