From 4155213878167e40bf19d2d9764107a5da0580d1 Mon Sep 17 00:00:00 2001 From: SySyAli Date: Wed, 3 Jan 2024 23:21:06 -0500 Subject: [PATCH] Adding comments to the IWindowInterface class. --- IWindowInterface.cpp | 86 ++++++++++++++++++++++---------------------- IWindowInterface.h | 6 +++- 2 files changed, 47 insertions(+), 45 deletions(-) diff --git a/IWindowInterface.cpp b/IWindowInterface.cpp index 93c18df..d514e63 100644 --- a/IWindowInterface.cpp +++ b/IWindowInterface.cpp @@ -1,6 +1,6 @@ /** * @file IWindowInterface.cpp - * @brief Implementation of AppTracker.h + * @brief Implementation of IWindowInterface.h * @author Syed Ali */ @@ -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 @@ -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); @@ -153,7 +117,9 @@ 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"); @@ -161,12 +127,44 @@ void AppTracker::startTrackingLinux() { 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( + 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 - - */ \ No newline at end of file +#endif \ No newline at end of file diff --git a/IWindowInterface.h b/IWindowInterface.h index 765538a..a5453fd 100644 --- a/IWindowInterface.h +++ b/IWindowInterface.h @@ -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();