Skip to content

Commit

Permalink
Adding comments to the IWindowInterface class.
Browse files Browse the repository at this point in the history
  • Loading branch information
SySyAli committed Jan 4, 2024
1 parent 72e9b14 commit 4155213
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
86 changes: 42 additions & 44 deletions IWindowInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @file IWindowInterface.cpp
* @brief Implementation of AppTracker.h
* @brief Implementation of IWindowInterface.h
* @author Syed Ali
*/

Expand Down Expand Up @@ -56,33 +56,16 @@ void WindowsTracker::startTracking()
HWND hwnd = GetForegroundWindow();
GetWindowText(hwnd, windowTitle, sizeof(windowTitle));
std::string title(windowTitle);
// time_t now = time(nullptr);
// // convert now to string form
// char dt[26];
//
// int err = ctime_s(dt, sizeof(dt), &now);
//
// if (err) {
// throw std::runtime_error("Error converting time");
// }

char dt[26];
auto curTime = std::chrono::system_clock::now();
const std::time_t t_c = std::chrono::system_clock::to_time_t(curTime);

// For Windows, use ctime_s
#ifdef _WIN32
// For Windows - ctime_s
int err = ctime_s(dt, sizeof(dt), &t_c);
if (err) {
throw std::runtime_error("Error converting time");
}
// For Unix systems (Linux, macOS), use ctime_r
#else
char* res_s = ctime_r(&t_c, dt);
if (!res_s) {
throw std::runtime_error("Error converting time");
}
#endif

if (!title.empty()) {
// handle the first entry
Expand Down Expand Up @@ -115,26 +98,7 @@ void WindowsTracker::startTracking()
}
#endif

void LinuxTracker::startTracking()
{
throw std::runtime_error("not implemented yet - linux");
}
/*
#ifdef _WIN32
// startTracking Method - windows implementation
void startTrackingWindows();
#elif __linux__
// startTracking Method - linux implementation
void startTrackingLinux();
// TODO: Linux implementation
// TODO: MacOS implementation
#endif
*/

/*
#if __linux__
#if __linux__
std::string getTrackingScreenOS(Display* display)
{
Display* display = XOpenDisplay(nullptr);
Expand All @@ -153,20 +117,54 @@ std::string getTrackingScreenOS(Display* display)
XFree(title);
return windowTitle;
}
void AppTracker::startTrackingLinux() {
void LinuxTracker::startTracking()
{
AppEntry prevEntry;
Display* display = XOpenDisplay(nullptr);
if (!display) {
throw std::runtime_error("Cannot open display");
}

while (tracking) {
std::string title = getActiveWindowTitle(display);
// ... rest of the tracking logic
char dt[26];
auto curTime = std::chrono::system_clock::now();
const std::time_t t_c = std::chrono::system_clock::to_time_t(curTime);

// unix based systems - ctime_r
char* res_s = ctime_r(&t_c, dt);
if (!res_s) {
throw std::runtime_error("Error converting time");
}
if (!title.empty()) {
// handle the first entry
if (prevEntry.isEmpty()) {
std::cout << "Starting time for " << title << " : " << dt;
prevEntry.setTitle(title);
prevEntry.setStartTime(curTime);
} else if (prevEntry.getTitle() != title) {
prevEntry.setEndTime(curTime);
// Calculate the duration in seconds
auto duration = std::chrono::duration_cast<std::chrono::seconds>(
prevEntry.getEndTime() - prevEntry.getStartTime());

std::cout << "Ending time for " << prevEntry.getTitle() << " : " << dt;
std::cout << "Duration for " << prevEntry.getTitle() << " : " << duration.count()
<< " seconds" << std::endl;

// insert the appEntry
dbManager.insertData(prevEntry);

// figure out the time spent at the website
std::cout << "Starting time for " << title << " : " << dt << std::endl;
prevEntry.setTitle(title);
prevEntry.setStartTime(curTime);
}
}

std::this_thread::sleep_for(std::chrono::seconds(1));
}

XCloseDisplay(display);
}
#endif
*/
#endif
6 changes: 5 additions & 1 deletion IWindowInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

#include "DatabaseManager.h"

// factory pattern
/**
* @class IWindowInterface
* @brief Helper Class containing the OS specific implementations of AppTracker.
* @author Syed Ali
*/
class IWindowInterface {
public:
IWindowInterface();
Expand Down

0 comments on commit 4155213

Please sign in to comment.