Skip to content

Commit

Permalink
All - Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Jan 2, 2024
1 parent 6eaae19 commit 51c18dd
Show file tree
Hide file tree
Showing 24 changed files with 135 additions and 127 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ add_library (${PROJECT_NAME}
src/appinfo.cpp
src/aura.cpp
src/configurationbase.cpp
src/dependencylocator.cpp
src/interprocesscommunicator.cpp
src/systemdirectories.cpp
src/userdirectories.cpp
Expand Down
30 changes: 10 additions & 20 deletions docs/aura.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ This module contains types and functions every Nickvision application utilizes.
- [AppInfo](#appinfo)
- [Aura](#aura)
- [ConfigurationBase](#configurationbase)
- [DependencyLocator](#dependencylocator)
- [EnumFlags](#enumflags)
- [InterProcessCommunicator](#interprocesscommunicator)
- [SystemDirectories](#systemdirectories)
Expand Down Expand Up @@ -148,6 +147,7 @@ Path: `Nickvision::Aura::Aura`
```
- Accepts: The string key of the config file, key.
- Returns: A reference to the configuration object of type T with key key.
- Throws: `std::invalid_argument` if key is empty
- Note: T must be a type that derives from `Nickvision::Aura::ConfigurationBase`
- Ex: `getConfig<Configuration>("config")` will return the `Configuration` object parsed from a `config.json` file on disk.

Expand Down Expand Up @@ -177,6 +177,14 @@ Path: `Nickvision::Aura::Aura`
- Returns: `true` if the environment variable of name key was set to value.
- Returns: `false` if setting the environment variable failed.
- Ex: `Aura::setEnvVar("AURA", "true")` will set `"$AURA:true"`.
- ```cpp
const std::filesystem::path& findDependency(std::string dependency)
```
- Accepts: The name of a dependency to find, dependency.
- Returns: The path of the dependency on disk if found.
- Returns: An empty path if the dependency was not found on disk.
- Ex: `Aura::findDependency("cmd")` on Windows will return `C:\Windows\System32\cmd.exe`.
- Ex: `Aura::findDependency("bash")` on Linux will return `/usr/bin/bash`.
## ConfigurationBase
Description: A base class for configuration files.
Expand Down Expand Up @@ -205,6 +213,7 @@ Path: `Nickvision::Aura::ConfigurationBase`
```
- Constructs the ConfigurationBase, loading the file from disk
- Accepts: The key of the configuration file, key
- Throws: `std::invalid_argument` if key is empty
- ```cpp
bool save()
```
Expand Down Expand Up @@ -264,25 +273,6 @@ int main()
}
```
## DependencyLocator
Description: Functions for working with dependencies.
Interface: [dependencylocator.h](/include/dependencylocator.h)
Type: `namespace`
Path: `Nickvision::Aura::DependencyLocator`
### Functions
- ```cpp
const std::filesystem::path& find(std::string dependency)
```
- Accepts: The name of a dependency to find, dependency.
- Returns: The path of the dependency on disk if found.
- Returns: An empty path if the dependency was not found on disk.
- Ex: `DependencyLocator::find("cmd")` on Windows will return `C:\Windows\System32\cmd.exe`.
- Ex: `DependencyLocator::find("bash")` on Linux will return `/usr/bin/bash`.

## EnumFlags
Description: Macros for working with enums to be used as flags.
Expand Down
2 changes: 1 addition & 1 deletion docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public:
bool save()
{
...
saved.invoke({});
saved({}); //Same as saved.invoke({});
...
}

Expand Down
9 changes: 9 additions & 0 deletions docs/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ Path: `Nickvision::Aura::Filesystem::FileSystemWatcher`
- Invoked when a watched flag of the folder is changed.

### Methods
- ```cpp
FileSystemWatcher(const std::filesystem::path& path, bool includeSubdirectories, WatcherFlags watcherFlags = WatcherFlags::FileName | WatcherFlags::DirectoryName | WatcherFlags::Attributes | WatcherFlags::Size | WatcherFlags::LastWrite | WatcherFlags::LastAccess)
```
- Constructs a FileSystemWatcher.
- Accepts: The path of the folder to watch, path, whether or not to watch subdirectories of said folder, includeSubdirectories, and the set of watcher flags, watcherFlags.
- ```cpp
~FileSystemWatcher()
```
- Destructs a FileSystemWatcher.
- ```cpp
bool isExtensionWatched(const std::filesystem::path& extension)
```
Expand Down
2 changes: 1 addition & 1 deletion docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Assume we want to create the following context menu for a `NotifyIcon`:
Here is the code we could use to accomplish this:
```cpp
...
bool m_running = true;
bool m_running{ true };
NotifyIconMenu contextMenu;
contextMenu.addAction("Show Window", [&]()
{
Expand Down
8 changes: 8 additions & 0 deletions docs/taskbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ Path: `Nickvision::Aura::Taskbar::TaskbarItem`
- Note: Setting the count will set the count visible to true if count > 0, else will set count visible to false.

### Methods
- ```cpp
TaskbarItem()
```
- Constructs a TaskbarItem.
- ```cpp
~TaskbarItem()
```
- Destructs a TaskbarItem.
- ```cpp
bool connect(HWND hwnd)
```
Expand Down
12 changes: 12 additions & 0 deletions include/aura.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include "appinfo.h"
#include "configurationbase.h"
Expand All @@ -30,11 +31,16 @@ namespace Nickvision::Aura
* @brief Gets a config object.
* @tparam T Derived type of ConfigurationBase
* @param key The key of the config file
* @throw std::invalid_argument Thrown if key is empty
* @return The config object
*/
template<DerivedConfigurationBase T>
T& getConfig(const std::string& key) noexcept
{
if (key.empty())
{
throw std::invalid_argument("Key must not be empty.");
}
if (!m_configFiles.contains(key))
{
m_configFiles[key] = std::make_unique<T>(key);
Expand Down Expand Up @@ -85,6 +91,12 @@ namespace Nickvision::Aura
* @return True if set, else false
*/
static bool setEnvVar(const std::string& key, const std::string& value) noexcept;
/**
* @brief Finds the path of a given dependency.
* @param dependency The name of the dependency to find
* @return The path of the dependency if found, else empty path
*/
static const std::filesystem::path& findDependency(std::string dependency) noexcept;

private:
static std::unique_ptr<Aura> m_instance;
Expand Down
5 changes: 3 additions & 2 deletions include/configurationbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ namespace Nickvision::Aura
/**
* @brief Constructs a ConfigurationBase, loading the file from disk.
* @param key The key of the config file
* @throw std::invalid_argument Thrown if key is empty
*/
ConfigurationBase(const std::string& key) noexcept;
ConfigurationBase(const std::string& key);
/**
* @brief Deconstructs a ConfigurationBase.
* @brief Destructs a ConfigurationBase.
*/
virtual ~ConfigurationBase() = default;
/**
Expand Down
17 changes: 0 additions & 17 deletions include/dependencylocator.h

This file was deleted.

2 changes: 1 addition & 1 deletion include/enumflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ inline T& operator&=(T& a, T b) noexcept \
inline T& operator^=(T& a, T b) noexcept \
{ \
return (T&)((int&)a ^= (int)b); \
} \
}

#endif //ENUMFLAGS_H
22 changes: 15 additions & 7 deletions include/events/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Nickvision::Aura::Events
m_handlers = std::move(e.m_handlers);
}
/**
* @brief Subscribes a handler to the event
* @brief Subscribes a handler to the event.
* @param handler The handler function
*/
void subscribe(const std::function<void(const T&)>& handler) noexcept
Expand All @@ -53,7 +53,7 @@ namespace Nickvision::Aura::Events
m_handlers.push_back(handler);
}
/**
* @brief Unsubscribes a handler from the event
* @brief Unsubscribes a handler from the event.
* @param handler The handler function
*/
void unsubscribe(const std::function<void(const T&)>& handler) noexcept
Expand All @@ -63,7 +63,7 @@ namespace Nickvision::Aura::Events
{
return;
}
auto it = std::find(m_handlers.begin(), m_handlers.end(), handler);
auto it{ std::find(m_handlers.begin(), m_handlers.end(), handler) };
if (it != m_handlers.end())
{
m_handlers.erase(it);
Expand All @@ -82,23 +82,31 @@ namespace Nickvision::Aura::Events
}
}
/**
* @brief Subscribes a handler to the event
* @brief Subscribes a handler to the event.
* @param handler The handler function
*/
void operator+=(const std::function<void(const T&)>& handler) noexcept
{
subscribe(handler);
}
/**
* @brief Unsubscribes a handler from the event
* @brief Unsubscribes a handler from the event.
* @param handler The handler function
*/
void operator-=(const std::function<void(const T&)>& handler) noexcept
{
unsubscribe(handler);
}
/**
* @brief Copies an Event
* @brief Invokes the event, calling all handlers.
* @param param The parameter to pass to the handlers
*/
void operator()(const T& param) noexcept
{
invoke(param);
}
/**
* @brief Copies an Event.
* @param e The Event to copy
* @return this
*/
Expand All @@ -113,7 +121,7 @@ namespace Nickvision::Aura::Events
return *this;
}
/**
* @brief Moves an Event
* @brief Moves an Event.
* @param e The Event to move
* @return this
*/
Expand Down
1 change: 0 additions & 1 deletion include/events/parameventargs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace Nickvision::Aura::Events
{

}

/**
* @brief Gets the param stored in the event args.
* @return The param stored
Expand Down
2 changes: 1 addition & 1 deletion include/filesystem/filesystemwatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Nickvision::Aura::Filesystem
*/
FileSystemWatcher(const std::filesystem::path& path, bool includeSubdirectories, WatcherFlags watcherFlags = WatcherFlags::FileName | WatcherFlags::DirectoryName | WatcherFlags::Attributes | WatcherFlags::Size | WatcherFlags::LastWrite | WatcherFlags::LastAccess);
/**
* @brief Deconstructs a FileSystemWatcher.
* @brief Destructs a FileSystemWatcher.
*/
~FileSystemWatcher() noexcept;
/**
Expand Down
2 changes: 1 addition & 1 deletion include/taskbar/taskbaritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Nickvision::Aura::Taskbar
*/
TaskbarItem() noexcept;
/**
* @brief Deconstructs a TaskbarItem.
* @brief Destructs a TaskbarItem.
*/
~TaskbarItem() noexcept;
/**
Expand Down
5 changes: 2 additions & 3 deletions src/appinfo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "appinfo.h"
#include <memory>
#include <sstream>
#include <maddy/parser.h>
#include "helpers/stringhelpers.h"
Expand Down Expand Up @@ -89,8 +88,8 @@ namespace Nickvision::Aura
markdown << StringHelpers::trim(line);
markdown << std::endl;
}
std::unique_ptr<maddy::Parser> parser{ std::make_unique<maddy::Parser>() };
m_htmlChangelog = parser->Parse(markdown);
maddy::Parser parser;
m_htmlChangelog = parser.Parse(markdown);
}

const std::string& AppInfo::getHtmlChangelog() const noexcept
Expand Down
44 changes: 43 additions & 1 deletion src/aura.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "aura.h"
#include <cstdlib>
#include <stdexcept>
#include <filesystem>
#include <mutex>
#include "systemdirectories.h"
#include "localization/gettext.h"
#include "helpers/stringhelpers.h"
#ifdef __linux__
Expand Down Expand Up @@ -67,4 +69,44 @@ namespace Nickvision::Aura
return setenv(key.c_str(), value.c_str(), true) == 0;
#endif
}

const std::filesystem::path& Aura::findDependency(std::string dependency) noexcept
{
static std::map<std::string, std::filesystem::path> locations;
static std::mutex mutex;
std::lock_guard<std::mutex> lock{ mutex };
#ifdef _WIN32
if (!std::filesystem::path(dependency).has_extension())
{
dependency += ".exe";
}
#endif
if (locations.contains(dependency))
{
const std::filesystem::path& location = locations[dependency];
if (std::filesystem::exists(location))
{
return location;
}
}
locations[dependency] = std::filesystem::path();
std::filesystem::path path{ std::filesystem::current_path() / dependency };
if (std::filesystem::exists(path))
{
locations[dependency] = path;
}
else
{
for (const std::filesystem::path& dir : SystemDirectories::getPath())
{
path = { dir / dependency };
if (std::filesystem::exists(path) && dir.string().find("AppData\\Local\\Microsoft\\WindowsApps") == std::string::npos)
{
locations[dependency] = path;
break;
}
}
}
return locations[dependency];
}
}
Loading

0 comments on commit 51c18dd

Please sign in to comment.