-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OS notification support (libnotify)
- Loading branch information
Showing
5 changed files
with
290 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# - Try to find Libnotify | ||
# Once done this will define | ||
# | ||
# LIBNOTIFY_FOUND - system has Libnotify | ||
# LIBNOTIFY_INCLUDE_DIR - the Libnotify include directory | ||
# LIBNOTIFY_LIBRARIES - Link these to use Libnotify | ||
# Redistribution and use is allowed according to the terms of the BSD license. | ||
# For details see the accompanying COPYING-CMAKE-SCRIPTS file. | ||
# | ||
|
||
# Copyright (c) 2010, Ni Hui <shuizhuyuanluo@126.com> | ||
# | ||
# Redistribution and use is allowed according to the terms of the BSD license. | ||
# For details see the accompanying COPYING-CMAKE-SCRIPTS file. | ||
|
||
if(LIBNOTIFY_INCLUDE_DIR AND LIBNOTIFY_LIBRARIES) | ||
# in cache already | ||
set(LIBNOTIFY_FOUND_QUIETLY TRUE) | ||
endif(LIBNOTIFY_INCLUDE_DIR AND LIBNOTIFY_LIBRARIES) | ||
|
||
if(NOT WIN32) | ||
find_package(PkgConfig) | ||
pkg_check_modules(PKG_LIBNOTIFY libnotify) | ||
if(PKG_LIBNOTIFY_FOUND) | ||
set(LIBNOTIFY_FOUND) | ||
endif(PKG_LIBNOTIFY_FOUND) | ||
endif(NOT WIN32) | ||
|
||
find_path(LIBNOTIFY_INCLUDE_DIR NAMES notify.h | ||
PATHS | ||
HINTS ${PKG_LIBNOTIFY_INCLUDE_DIRS} | ||
PATH_SUFFIXES libnotify | ||
) | ||
|
||
find_library(LIBNOTIFY_LIBRARIES NAMES notify | ||
PATHS | ||
HINTS ${PKG_LIBNOTIFY_LIBRARY_DIRS} | ||
) | ||
|
||
if(LIBNOTIFY_INCLUDE_DIR AND LIBNOTIFY_LIBRARIES) | ||
set(LIBNOTIFY_FOUND TRUE) | ||
else(LIBNOTIFY_INCLUDE_DIR AND LIBNOTIFY_LIBRARIES) | ||
set(LIBNOTIFY_FOUND FALSE) | ||
endif(LIBNOTIFY_INCLUDE_DIR AND LIBNOTIFY_LIBRARIES) | ||
|
||
if(LIBNOTIFY_FOUND) | ||
include(CheckCSourceCompiles) | ||
find_package(GTK2 REQUIRED) | ||
# check whether we are using libnotify 0.4 | ||
set(CMAKE_REQUIRED_INCLUDES ${LIBNOTIFY_INCLUDE_DIR} ${GLIB2_INCLUDE_DIR} ${GLIB2_INTERNALE_DIR} ${GTK2_INCLUDE_DIRS}) | ||
set(CMAKE_REQUIRED_LIBRARIES ${LIBNOTIFY_LIBRARIES} ${GLIB2_LIBRARIES} ${GTK2_LIBRARIES}) | ||
check_c_source_compiles(" | ||
#include <notify.h> | ||
int main() | ||
{ | ||
notify_notification_new(NULL,NULL,NULL,NULL); | ||
return 0; | ||
} | ||
" HAVE_LIBNOTIFY_0_4) | ||
check_c_source_compiles(" | ||
#include <notify.h> | ||
int main() | ||
{ | ||
notify_notification_new(NULL,NULL,NULL); | ||
return 0; | ||
} | ||
" HAVE_LIBNOTIFY_0_7) | ||
set(CMAKE_REQUIRED_INCLUDES) | ||
set(CMAKE_REQUIRED_LIBRARIES) | ||
if(HAVE_LIBNOTIFY_0_4) | ||
message(STATUS "Found Libnotify: ${LIBNOTIFY_LIBRARIES}, (>= 0.4)") | ||
endif(HAVE_LIBNOTIFY_0_4) | ||
if(HAVE_LIBNOTIFY_0_7) | ||
message(STATUS "Found Libnotify: ${LIBNOTIFY_LIBRARIES}, (>= 0.7)") | ||
endif(HAVE_LIBNOTIFY_0_7) | ||
endif(LIBNOTIFY_FOUND) | ||
|
||
set(LIBNOTIFY_INCLUDE_DIR ${LIBNOTIFY_INCLUDE_DIR} CACHE INTERNAL "The Libnotify include path") | ||
set(LIBNOTIFY_LIBRARIES ${LIBNOTIFY_LIBRARIES} CACHE INTERNAL "The Libnotify library") | ||
set(HAVE_LIBNOTIFY_0_4 ${HAVE_LIBNOTIFY_0_4} CACHE INTERNAL "Whether the version of Libnotify is >= 0.4") | ||
set(HAVE_LIBNOTIFY_0_7 ${HAVE_LIBNOTIFY_0_7} CACHE INTERNAL "Whether the version of Libnotify is >= 0.7") | ||
|
||
mark_as_advanced(LIBNOTIFY_INCLUDE_DIR LIBNOTIFY_LIBRARIES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
grive: an GPL program to sync a local directory with Google Drive | ||
Copyright (C) 2012 Wan Wai Ho | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
as published by the Free Software Foundation version 2 | ||
of the License. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
AUTHORS++ | ||
Notification Handler by Daniele "OpenNingia" Simonetti | ||
*/ | ||
|
||
|
||
#include "NotificationHandler.hh" | ||
#include <libnotify/notify.h> | ||
#include <sstream> | ||
|
||
namespace gr { | ||
|
||
NotifyItem::NotifyItem(std::string name, std::string type) | ||
: m_name(name), m_type(type) | ||
{ | ||
} | ||
|
||
const std::string NotifyItem::name() const | ||
{ | ||
return m_name; | ||
} | ||
|
||
const std::string NotifyItem::type() const | ||
{ | ||
return m_type; | ||
} | ||
|
||
std::list<NotifyItem> Notify::s_cache; | ||
|
||
Notify::Notify(std::string name) | ||
: m_name(name) | ||
{ | ||
notify_init(name.c_str()); | ||
} | ||
|
||
Notify::~Notify() | ||
{ | ||
notify_uninit(); | ||
} | ||
|
||
void Notify::Emit(std::string message, std::string stock_icon, std::string categ, int tmo) | ||
{ | ||
NotifyNotification* ntf = notify_notification_new(m_name.c_str(), message.c_str(), stock_icon.c_str()); | ||
notify_notification_set_timeout(ntf, 3000); | ||
notify_notification_set_category(ntf, categ.c_str()); | ||
notify_notification_set_urgency (ntf, NOTIFY_URGENCY_NORMAL); | ||
GError *error = NULL; | ||
notify_notification_show(ntf, &error); | ||
} | ||
|
||
void Notify::Emit(NotifyItem itm) | ||
{ | ||
std::stringstream ss; | ||
if ( itm.type() == "download" ) | ||
{ | ||
ss << "Downloaded " << itm.name(); | ||
Notify().Emit(ss.str(), "download", "sync"); | ||
} | ||
else if (itm.type() == "delete") | ||
{ | ||
ss << "Deleted " << itm.name(); | ||
Notify().Emit(ss.str(), "delete", "sync"); | ||
} | ||
else if (itm.type() == "create") | ||
{ | ||
ss << "Created " << itm.name(); | ||
Notify().Emit(ss.str(), "new", "sync"); | ||
} | ||
else if (itm.type() == "changed") | ||
{ | ||
ss << "Uploaded changes in " << itm.name(); | ||
Notify().Emit(ss.str(), "upload", "sync"); | ||
} | ||
else | ||
{ | ||
ss << itm.name() << " is now in sync"; | ||
Notify().Emit(ss.str(), "download", "sync"); | ||
} | ||
} | ||
|
||
void Notify::Push(std::string name, std::string type) | ||
{ | ||
s_cache.push_back(NotifyItem(name, type)); | ||
} | ||
|
||
void Notify::Commit() | ||
{ | ||
if ( s_cache.size() == 0 ) | ||
return; | ||
|
||
if ( s_cache.size() == 1 ) | ||
Notify().Emit(s_cache.front()); | ||
else | ||
{ | ||
std::stringstream ss; | ||
ss << "Synchonized " << s_cache.size() << " files"; | ||
Notify().Emit(ss.str(), "download", "sync"); | ||
} | ||
s_cache.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
grive: an GPL program to sync a local directory with Google Drive | ||
Copyright (C) 2012 Wan Wai Ho | ||
This program is free software; you can redistribute it and/or | ||
modify it under the terms of the GNU General Public License | ||
as published by the Free Software Foundation version 2 | ||
of the License. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program; if not, write to the Free Software | ||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
AUTHORS++ | ||
Notification Handler by Daniele "OpenNingia" Simonetti | ||
*/ | ||
|
||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <list> | ||
|
||
namespace gr { | ||
|
||
class NotifyItem | ||
{ | ||
public: | ||
|
||
NotifyItem(std::string name, std::string type); | ||
|
||
const std::string name() const; | ||
const std::string type() const; | ||
|
||
private: | ||
std::string m_name; | ||
std::string m_type; | ||
}; | ||
|
||
class Notify | ||
{ | ||
|
||
public : | ||
Notify(std::string name = "Grive"); | ||
~Notify(); | ||
|
||
void Emit(std::string message, std::string stock_icon = "", std::string categ = "Info", int tmo = 3000); | ||
void Emit(NotifyItem itm); | ||
|
||
static void Push(std::string name, std::string type); | ||
static void Commit(); | ||
|
||
private: | ||
std::string m_name; | ||
|
||
static std::list<NotifyItem> s_cache; | ||
}; | ||
} |