Created
January 20, 2010 17:52
-
-
Save alno/282046 to your computer and use it in GitHub Desktop.
Akonadi application to add todos to calendar
This file contains hidden or 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
#include "add_todo.h" | |
#include <QTextStream> | |
#include <QStringList> | |
#include <Akonadi/Collection> | |
#include <Akonadi/CollectionFetchJob> | |
#include <Akonadi/Item> | |
#include <Akonadi/ItemCreateJob> | |
#include <kcal/todo.h> | |
#include <boost/shared_ptr.hpp> | |
using namespace Akonadi; | |
static QTextStream out( stdout ); // Поток для вывода данных | |
static QString todoMimeType( "text/calendar" ); // MIME-тип задачи | |
AddTodo::AddTodo( int argc, char ** argv ) : QCoreApplication( argc, argv ) { | |
out << "Application started" << endl; | |
CollectionFetchJob *job = new CollectionFetchJob( Collection::root(), CollectionFetchJob::FirstLevel, this ); // Создаем задачу | |
connect( job, SIGNAL(result(KJob*)), this, SLOT(collectionsFetched(KJob*)) ); // Связываем сигнал и слот | |
} | |
void AddTodo::collectionsFetched( KJob * job ) { | |
out << "Collections fetched" << endl; | |
if ( job->error() ) { | |
out << "Error occurred: " << job->errorText() << endl; | |
exit( -1 ); | |
return; | |
} | |
const CollectionFetchJob * fetchJob = qobject_cast<CollectionFetchJob*>( job ); // Приводим задачу к нужному типу | |
const Collection * selectedCollection = 0; // Переменная для выбранной коллекции | |
foreach( const Collection & collection, fetchJob->collections() ) { | |
out << "Name: " << collection.name(); // Печатаем имя коллекции для отладки | |
if ( collection.contentMimeTypes().contains( todoMimeType ) ) { // Проверяем, принимает ли коллекция нужный тип данных | |
selectedCollection = &collection; | |
break; | |
} | |
} | |
if ( !selectedCollection ) { // Если не нашли подходящей коллекции, то печатаем ошибку и выходим | |
out << "Error occurred: no valid collection found"<< endl; | |
exit( -1 ); | |
return; | |
} | |
// А здесь будем создавать задачу | |
KDateTime dueDate = KDateTime::fromString( arguments()[2], "%d.%m.%Y" ); // Парсим дату | |
if ( !dueDate.isValid() ) { // Проверяем, что дата распарсилась | |
out << "Error occured: invalid date '" << arguments()[2] << "'" << endl; | |
exit( -2 ); | |
} | |
KCal::Todo::Ptr todo( new KCal::Todo() ); | |
todo->setSummary( arguments()[1] ); // Текст | |
todo->setDtDue( dueDate ); // Дата | |
todo->setPercentComplete( 0 ); // Пока не выполнена | |
todo->setHasStartDate( false ); // Начальная дата не установлена | |
todo->setHasDueDate( true ); // Установлена дата выполнения | |
Item item( todoMimeType ); | |
item.setPayload<KCal::Todo::Ptr>( todo ); | |
ItemCreateJob * itemCreateJob = new ItemCreateJob( item, *selectedCollection, this ); // Создаем задачу | |
connect( itemCreateJob, SIGNAL(result(KJob*)), this, SLOT(todoCreated(KJob*)) ); // Связываем сигнал и слот | |
} | |
void AddTodo::todoCreated( KJob * job ) { | |
if ( job->error() ) { | |
out << "Error occurred: " << job->errorText() << endl; | |
exit( -1 ); | |
return; | |
} | |
out << "TODO created" << endl; | |
quit(); | |
} | |
int main( int argc, char ** argv ) { | |
if ( argc < 3 ) { // Проверяем количество аргументов | |
out << "Usage: add-todo [text] [date]" << endl; | |
return -2; | |
} | |
AddTodo app( argc, argv ); // Создаем экземпляр приложения | |
return app.exec(); // И входим в цикл обработки сигналов | |
} |
This file contains hidden or 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
#ifndef ADD_TODO_H | |
#define ADD_TODO_H | |
#include <QCoreApplication> | |
#include <KJob> | |
class AddTodo : public QCoreApplication { | |
Q_OBJECT | |
public: | |
AddTodo( int argc, char ** argv ); | |
public slots: | |
void collectionsFetched( KJob * job ); // Будет вызван, когда мы получим список коллекций | |
void todoCreated( KJob * job ); // Будет вызван, когда мы создадим задачу | |
}; | |
#endif // ADD_TODO_H |
This file contains hidden or 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
PROJECT(add-todo) | |
find_package(KDE4 REQUIRED) # Находим модули KDE4 | |
find_package(KdepimLibs REQUIRED) # Находим модули KDE PIM | |
include(KDE4Defaults) | |
add_definitions (${QT_DEFINITIONS} ${KDE4_DEFINITIONS}) | |
include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR} ${KDEPIMLIBS_INCLUDE_DIR} ${KDE4_INCLUDES}) | |
set(CMAKE_CXX_FLAGS "-fexceptions") | |
kde4_add_executable(add-todo add_todo.cpp) # Добавляем цель | |
target_link_libraries(add-todo ${KDE4_PLASMA_LIBS} ${KDE4_KDEUI_LIBS} ${KDE4_AKONADI_LIBS} ${KDEPIMLIBS_KCAL_LIBS}) # Добавляем соответствующие библиотеки |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment