Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying the DB for next events #128

Merged
merged 9 commits into from
Sep 10, 2022
126 changes: 112 additions & 14 deletions daemon/src/CalendarDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ int main()

CalendarDaemon::CalendarDaemon()
:
BApplication(kApplicationSignature)
BApplication(kApplicationSignature),
fEventList()
harshit-sharma-gits marked this conversation as resolved.
Show resolved Hide resolved
{
std::cout << "Creating Daemon..." << std::endl;

Expand All @@ -55,8 +56,22 @@ CalendarDaemon::CalendarDaemon()
node_ref nodeRef;
directory.GetNodeRef(&nodeRef);
watch_node(&nodeRef, B_WATCH_DIRECTORY, be_app_messenger);

entry_ref ref;
while (directory.GetNextRef(&ref) == B_OK) {
BNode node(&ref);
node_ref nodeRef;
node.GetNodeRef(&nodeRef);
watch_node(&nodeRef, B_WATCH_ATTR, be_app_messenger);
}
} else
std::cout << "Events Directory not found!!" << std::endl;

fDBManager = new QueryDBManager();

fEventList = fDBManager->GetEventsToNotify(BDateTime::CurrentDateTime(B_LOCAL_TIME));
fEventList->SortItems(_CompareFunction);
ShowEvents();
}


Expand All @@ -75,20 +90,31 @@ CalendarDaemon::MessageReceived(BMessage *message)
{
std::cout << "\nNode Monitor Message Received!" << std::endl;
int32 opCode;

if (message->FindInt32("opcode", &opCode) == B_OK) {
switch (opCode) {
case B_ENTRY_CREATED:
std::cout << "New Event Created!" << std::endl;
break;
case B_ENTRY_REMOVED:
case B_ENTRY_MOVED:
std::cout << "An Event Removed!" << std::endl;
break;
}
ino_t node;
message->FindInt32("opcode", &opCode);
message->FindInt64("node", &node);

switch (opCode) {
case B_ENTRY_CREATED: {
std::cout << "New Event Created!" << std::endl;
entry_ref ref;
const char* name;

message->FindInt32("device", &ref.device);
message->FindInt64("directory", &ref.directory);
message->FindString("name", &name);
ref.set_name(name);

AddEventToList(&ref);
} break;
//case B_ENTRY_REMOVED:
case B_ENTRY_MOVED:
std::cout << "An Event Removed!" << std::endl;
break;
case B_ATTR_CHANGED:
std::cout << "Attribute Changed!" << std::endl;
break;
}
else
std::cout << "Op Code not found!" << std::endl;
break;
}
case B_QUIT_REQUESTED:
Expand All @@ -106,3 +132,75 @@ CalendarDaemon::QuitRequested()
{
return true;
}


void
CalendarDaemon::WatchEvent(entry_ref* ref)
{
BNode node(ref);
node_ref nodeRef;
node.GetNodeRef(&nodeRef);
watch_node(&nodeRef, B_WATCH_ATTR, be_app_messenger);
}


void
CalendarDaemon::UnwatchEvent(entry_ref* ref)
{
BNode node(ref);
node_ref nodeRef;
node.GetNodeRef(&nodeRef);
watch_node(&nodeRef, B_STOP_WATCHING, be_app_messenger);
}


void
CalendarDaemon::AddEventToList(entry_ref* ref)
{
Event* event = fDBManager->FileToEvent(ref);
fEventList->AddItem(event);
fEventList->SortItems(_CompareFunction);
WatchEvent(ref);
ShowEvents();
}


void
CalendarDaemon::RemoveEventFromList(entry_ref* ref)
{
BNode node(ref);
node_ref nodeRef;
node.GetNodeRef(&nodeRef);
watch_node(&nodeRef, B_STOP_WATCHING, be_app_messenger);
}


void
CalendarDaemon::ShowEvents()
{
if (fEventList->IsEmpty()) {
std::cout << "The List is empty!" << std::endl;
return;
}

Event* event;
std::cout << std::endl;
for (int32 i=0 ; i<fEventList->CountItems() ; ++i) {
std::cout << "hue\n";
event = fEventList->ItemAt(i);
std::cout << "Event Name: " << event->GetName() << "\n";
std::cout << "Event Place: " << event->GetPlace() << "\n\n";
}
}


int
CalendarDaemon::_CompareFunction(const Event* a, const Event* b)
{
if (difftime(a->GetReminderTime(), b->GetReminderTime()) < 0)
return -1;
else if (difftime(a->GetReminderTime(), b->GetReminderTime()) > 0)
return 1;
else
return 0;
}
12 changes: 12 additions & 0 deletions daemon/src/CalendarDaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <OS.h>
#include <Volume.h>

#include "QueryDBManager.h"

/*!
CalendarDaemon Class Declaration
Expand All @@ -25,10 +26,21 @@ class CalendarDaemon : public BApplication
void MessageReceived(BMessage* message);
bool QuitRequested();

void WatchEvent(entry_ref* ref);
void UnwatchEvent(entry_ref* ref);
void ShowEvents();
void AddEventToList(entry_ref* ref);
void RemoveEventFromList(entry_ref* ref);

private:

static int _CompareFunction(const Event* a, const Event* b);

BString fEventDir;
BVolume fQueryVolume;
EventList* fEventList;
QueryDBManager* fDBManager;
harshit-sharma-gits marked this conversation as resolved.
Show resolved Hide resolved

};

#endif
25 changes: 10 additions & 15 deletions main/src/db/QueryDBManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ QueryDBManager::UpdateNotifiedEvent(const char* id)
if (_EventStatusSwitch(evFile.InitCheck()) != B_OK)
return NULL;

Event* event = _FileToEvent(&ref);
Event* event = FileToEvent(&ref);
event->SetStatus(event->GetStatus() | EVENT_NOTIFIED);
return _EventToFile(event, &evFile);
}
Expand Down Expand Up @@ -194,7 +194,7 @@ QueryDBManager::RestoreEvent(entry_ref ref)
entry.GetName(oldLeaf);
BString leaf = _UniqueFilename(parentDir, BString(oldLeaf));

Event* event = _FileToEvent(&ref);
Event* event = FileToEvent(&ref);
if (event != NULL) {
BString eventName = _UniqueEventName(
event->GetName(), event->GetStartDateTime(), event->GetId());
Expand All @@ -217,7 +217,7 @@ QueryDBManager::GetEvent(const char* id)
{
entry_ref ref;
_GetFileOfId(id, NULL, &ref);
return _FileToEvent(&ref);
return FileToEvent(&ref);
}


Expand All @@ -232,7 +232,7 @@ QueryDBManager::GetEvent(const char* name, time_t startTime)
Event*
QueryDBManager::GetEvent(entry_ref ref)
{
return _FileToEvent(&ref);
return FileToEvent(&ref);
}


Expand Down Expand Up @@ -292,7 +292,7 @@ QueryDBManager::GetEventsInInterval(time_t start, time_t end, bool ignoreHidden)
Event* event;

while (query.GetNextRef(&ref) == B_OK) {
event = _FileToEvent(&ref);
event = FileToEvent(&ref);
uint16 status = event->GetStatus();
bool hidden = (status & EVENT_DELETED) || (status & EVENT_HIDDEN);
if (ignoreHidden == false || ignoreHidden == true && hidden == false)
Expand Down Expand Up @@ -322,7 +322,7 @@ QueryDBManager::GetEventsOfCategory(Category* category)
while (query.GetNextRef(&ref) == B_OK) {
if (fTrashDir->Contains(BPath(&ref).Path()) == true)
continue;
event = _FileToEvent(&ref);
event = FileToEvent(&ref);
events->AddItem(event);
}
return events;
Expand All @@ -338,14 +338,9 @@ QueryDBManager::GetEventsToNotify(BDateTime dateTime)

time_t time = dateTime.Time_t();

query.PushAttr("Event:Start");
query.PushAttr("Event:Reminder");
query.PushUInt32(time);
query.PushOp(B_LE);

query.PushAttr("Event:Status");
query.PushString("Unnotified");
query.PushOp(B_CONTAINS);
query.PushOp(B_AND);
query.PushOp(B_GE);

query.Fetch();
entry_ref ref;
Expand All @@ -356,7 +351,7 @@ QueryDBManager::GetEventsToNotify(BDateTime dateTime)
while (query.GetNextRef(&ref) == B_OK) {
if (fTrashDir->Contains(BPath(&ref).Path()) == true)
continue;
event = _FileToEvent(&ref);
event = FileToEvent(&ref);
events->AddItem(event);
}
return events;
Expand Down Expand Up @@ -589,7 +584,7 @@ QueryDBManager::_FileToCategory(BFile* file)


Event*
QueryDBManager::_FileToEvent(entry_ref* ref)
QueryDBManager::FileToEvent(entry_ref* ref)
{
BNode node(ref);
BEntry entry(ref);
Expand Down
3 changes: 2 additions & 1 deletion main/src/db/QueryDBManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class QueryDBManager
bool RemoveCategory(Category* category);
bool RemoveCategory(entry_ref categoryRef);

Event* FileToEvent(entry_ref* ref);
harshit-sharma-gits marked this conversation as resolved.
Show resolved Hide resolved

private:
void _Initialize();

Expand All @@ -69,7 +71,6 @@ class QueryDBManager
status_t _GetFileOfId(const char* id, BFile* file, entry_ref* ref = NULL);

Category* _FileToCategory(BFile* file);
Event* _FileToEvent(entry_ref* ref);
bool _CategoryToFile(Category* category, BFile* file);
bool _EventToFile(Event* event, BFile* file);

Expand Down