Skip to content

Commit

Permalink
Fixed Live Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
harshit-sharma-gits committed Aug 9, 2022
1 parent 3d7b13c commit 0b11b2b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 36 deletions.
111 changes: 76 additions & 35 deletions daemon/src/CalendarDaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define DESCRIPTION_ATTR "Event:Description"
#define PLACE_ATTR "Event:Place"
#define NAME_ATTR "Event:Name"
#define STATUS_ATTR "Event:Status"
#define CATNAME_ATTR "Event:Category"

const char* kApplicationSignature = "application/x-vnd.CalendarDaemon";
Expand Down Expand Up @@ -62,6 +63,7 @@ ReminderEvent::ReminderEvent(const char* name, const char* place,
fDescription = BString(description);
fCatName = BString(catName);
fStart = start;
//fStatus = status;
}


Expand All @@ -72,6 +74,7 @@ ReminderEvent::ReminderEvent(ReminderEvent &event)
fDescription = event.GetDescription();
fCatName = event.GetCatName();
fStart = event.GetStartDateTime();
fStatus = event.GetStatus();
}


Expand Down Expand Up @@ -145,6 +148,20 @@ ReminderEvent::SetCatName(const char* catName)
}


uint16
ReminderEvent::GetStatus() const
{
return fStatus;
}


void
ReminderEvent::SetStatus(const uint16 status)
{
fStatus = status;
}



/*!
CalendarDaemon Class Definitions
Expand All @@ -166,13 +183,19 @@ CalendarDaemon::CalendarDaemon()
BVolumeRoster volRoster;
volRoster.GetBootVolume(&fQueryVolume);

BPath homeDir;
find_directory(B_USER_DIRECTORY, &homeDir);
BPath homePath;
find_directory(B_USER_DIRECTORY, &homePath);
fEventDir = new BDirectory(homePath.Path());

//fEventDir = homeDir.Path();
//fEventDir << "/" << EVENT_DIRECTORY;

fEventDir = homeDir.Path();
fEventDir << "/" << EVENT_DIRECTORY;
//BDirectory directory(fEventDir.String());

BPath trashPath;
find_directory(B_TRASH_DIRECTORY, &trashPath);
fTrashDir = new BDirectory(trashPath.Path());

BDirectory directory(fEventDir.String());
/*if(directory.InitCheck() == B_ENTRY_NOT_FOUND)
{
std::cerr << "Failed to access the Events Directory" << std::endl;
Expand All @@ -184,36 +207,45 @@ CalendarDaemon::CalendarDaemon()
AddEventToList(&ref);
}*/

BQuery query;
query.SetVolume(&fQueryVolume);
query.PushAttr(START_ATTR);
query.PushUInt32(time(NULL));
query.PushOp(B_GE);

if(query.SetTarget(be_app_messenger) != B_OK)
std::cout << "Query Target not set" << std::endl;

query.Fetch();
entry_ref ref;

while(query.GetNextRef(&ref) == B_OK)
AddEventToList(&ref);

BNotification notification(B_INFORMATION_NOTIFICATION);
notification.SetTitle("Calendar Daemon is Up & Running!");
notification.SetContent("Secretly Monitoring your Events!");

notification.Send();

node_ref nodeRef;
directory.GetNodeRef(&nodeRef);
watch_node(&nodeRef, B_WATCH_DIRECTORY | B_WATCH_ATTR, be_app_messenger);
fEventDir->GetNodeRef(&nodeRef);
watch_node(&nodeRef, B_WATCH_DIRECTORY, be_app_messenger);

fEventLoop = spawn_thread(EventLoop, "EventLoop", B_NORMAL_PRIORITY, this);
resume_thread(fEventLoop);
}


void
CalendarDaemon::ReadyToRun()
{
LockEvents();

fQuery.SetVolume(&fQueryVolume);
fQuery.PushAttr(START_ATTR);
fQuery.PushUInt32(time(NULL));
fQuery.PushOp(B_GE);

if(fQuery.SetTarget(this) != B_OK)
std::cout << "Query Target not set" << std::endl;

fQuery.Fetch();
entry_ref ref;

while(fQuery.GetNextRef(&ref) == B_OK)
AddEventToList(&ref);
ShowEvents();

UnlockEvents();
}


CalendarDaemon::~CalendarDaemon()
{
std::cout << "Stopping Daemon, Clearing Memory, Good Bye! ;)" << std::endl;
Expand All @@ -234,12 +266,13 @@ CalendarDaemon::MessageReceived(BMessage *message)
{
case B_NODE_MONITOR:
{
std::cout << "Events Changed - Node Monitor\nRefreshing List" << std::endl;
RefreshEventList();
//std::cout << "Events Changed - Node Monitor\nRefreshing List" << std::endl;
//RefreshEventList();
break;
}
case B_QUERY_UPDATE:
std::cout << "Events Changed - Live Query" << std::endl;
RefreshEventList();
break;
case B_QUIT_REQUESTED:
QuitRequested();
Expand All @@ -255,7 +288,11 @@ void
CalendarDaemon::AddEventToList(entry_ref* ref)
{
ReminderEvent* event = _FileToReminderEvent(ref);
fEventList.AddItem(event);

BEntry evEntry(ref);
bool inTrash = fTrashDir->Contains(&evEntry);
if(!inTrash)
fEventList.AddItem(event);
}


Expand Down Expand Up @@ -315,38 +352,42 @@ CalendarDaemon::RefreshEventList()
{
LockEvents();

BQuery query;
query.Clear();
query.SetVolume(&fQueryVolume);
query.PushAttr(START_ATTR);
query.PushUInt32(time(NULL));
query.PushOp(B_GE);
query.Fetch();
fQuery.Clear();
fQuery.SetVolume(&fQueryVolume);
fQuery.PushAttr(START_ATTR);
fQuery.PushUInt32(time(NULL));
fQuery.PushOp(B_GE);

if(fQuery.SetTarget(this) != B_OK)
std::cout << "Query Target not set" << std::endl;

fQuery.Fetch();
entry_ref ref;
fEventList.MakeEmpty();

while(query.GetNextRef(&ref) == B_OK)
while(fQuery.GetNextRef(&ref) == B_OK)
AddEventToList(&ref);
fEventList.SortItems(_CompareFunction);

std::cout << "Unlocking after refreshing\n";
ShowEvents();
UnlockEvents();

if(fEventLoop)
Notify();
std::cout << fEventList.CountItems() << std::endl;
}


void
CalendarDaemon::ShowEvents()
{
ReminderEvent* event;
std::cout << std::endl;
for(int32 i=0 ; i<fEventList.CountItems() ; ++i)
{
event = fEventList.ItemAt(i);
std::cout << "Event Name: " << event->GetName() << "\n";
std::cout << "Event Name: " << event->GetDescription() << "\n\n";
std::cout << "Event Place: " << event->GetPlace() << "\n\n";
}
delete(event);
}
Expand Down
10 changes: 9 additions & 1 deletion daemon/src/CalendarDaemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BMessage;
class BString;
class BVolume;
class BQuery;
class BDirectory;


/*!
Expand Down Expand Up @@ -53,6 +54,9 @@ class ReminderEvent

const char* GetCatName() const;
void SetCatName(const char* catName);

uint16 GetStatus() const;
void SetStatus(const uint16 status);

private:
BString fName;
Expand All @@ -61,6 +65,7 @@ class ReminderEvent
BString fCatName;

time_t fStart;
uint16 fStatus;
};

typedef BObjectList<ReminderEvent> ReminderEventList;
Expand All @@ -78,6 +83,7 @@ class CalendarDaemon : public BApplication

void MessageReceived(BMessage* message);
bool QuitRequested();
void ReadyToRun();


void AddEventToList(entry_ref* ref);
Expand All @@ -95,13 +101,15 @@ class CalendarDaemon : public BApplication
ReminderEvent* _FileToReminderEvent(entry_ref* ref);
static int _CompareFunction(const ReminderEvent* a, const ReminderEvent* b);

BString fEventDir;
BDirectory* fEventDir;
BDirectory* fTrashDir;
ReminderEventList fEventList;
BVolume fQueryVolume;
sem_id fEventLock;
sem_id fNotify;
thread_id fEventLoop;
bool fQuitting;
BQuery fQuery;
};

int main();
Expand Down

0 comments on commit 0b11b2b

Please sign in to comment.