Add OS notification support (libnotify)

pull/40/head
daniele 2012-05-21 17:54:27 +02:00 committed by Matchman Green
parent 2fc4ffd1bf
commit 428afa6538
5 changed files with 290 additions and 6 deletions

View File

@ -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)

View File

@ -27,6 +27,7 @@
#include "http/XmlResponse.hh"
#include "protocol/Json.hh"
#include "protocol/OAuth2.hh"
#include "util/NotificationHandler.hh"
#include "util/Destroy.hh"
#include "util/Log.hh"
#include "xml/Node.hh"
@ -172,6 +173,7 @@ void Drive::Update()
http::Agent http ;
std::for_each( m_state.begin(), m_state.end(),
boost::bind( &Resource::Sync, _1, &http, m_http_hdr ) ) ;
Notify::Commit();
}
} // end of namespace

View File

@ -29,6 +29,7 @@
#include "util/Log.hh"
#include "util/OS.hh"
#include "util/StdioFile.hh"
#include "util/NotificationHandler.hh"
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
@ -37,6 +38,9 @@
// for debugging
#include <iostream>
// string strema
#include <sstream>
namespace gr {
/// default constructor creates the root folder
@ -213,7 +217,7 @@ Resource* Resource::FindChild( const std::string& name )
}
return 0 ;
}
// try to change the state to "sync"
void Resource::Sync( http::Agent *http, const http::Headers& auth )
{
@ -229,24 +233,36 @@ void Resource::Sync( http::Agent *http, const http::Headers& auth )
Log( "sync %1% %2% doesn't exist in server. upload \"%3%\"?",
m_entry.Title(), m_entry.Filename(), m_parent->m_entry.CreateLink(), log::verbose ) ;
if ( Create( http, auth ) )
{
m_state = sync ;
Notify::Push(m_entry.Filename(), "create");
}
break ;
case local_deleted :
Log( "sync %1% deleted in local. delete?", m_entry.Filename(), log::verbose ) ;
Log( "sync %1% deleted in local. delete?", m_entry.Filename(), log::verbose );
//Notify::Push(m_entry.Filename(), "delete");
break ;
case local_changed :
Log( "sync %1% changed in local", m_entry.Filename(), log::verbose ) ;
if ( EditContent( http, auth ) )
if ( EditContent( http, auth ) )
{
m_state = sync ;
Notify::Push(m_entry.Filename(), "changed");
}
break ;
case remote_new :
case remote_changed :
Log( "sync %1% changed in remote. download?", m_entry.Filename(), log::verbose ) ;
Download( http, Path(), auth ) ;
m_state = sync ;
{
Log( "sync %1% changed in remote. download?", m_entry.Filename(), log::verbose ) ;
Download( http, Path(), auth ) ;
m_state = sync ;
// OS NOTIFY
Notify::Push(m_entry.Filename(), "download");
}
break ;
case sync :

View File

@ -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();
}
}

View File

@ -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;
};
}