Skip to content
This repository has been archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
#115 Add platform class
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDuquesnoy committed Sep 17, 2017
1 parent cfabd2a commit 6c166cf
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 0 deletions.
71 changes: 71 additions & 0 deletions docs/developers/PlantUml/FilterPluginsByPlatform.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@startuml

package MellowPlayer::Application {
interface IStreamingServiceLoader {
+ load()
}
}

package MellowPlayer::Infrastructure {
class StreamingServiceLoader {
+ load()
- _filtersFactory: IPlatformFiltersFactory
- _platform: IPlatform
}

IStreamingServiceLoader <|-- StreamingServiceLoader

StreamingServiceLoader -left-> IPlatformFiltersFactory: _filtersFactory
StreamingServiceLoader -right-> IPlatform: _platform

interface IPlatform {
+ name(): QString
--
+ isLinux() : bool
+ isAppImage() : bool
+ isWindows() : bool
+ isOsx() : bool
--
+ match(QList<IPlatformFilter>): bool
}

class Platform
IPlatform <|-- Platform

Platform .left.> IPlatformFilter: <<match>>

interface IPlatformFiltersFactory {
+ create(QString) : QList<IPlatformFilter>
}

class PlatformFiltersFactory {
+ create(QString) : QList<IPlatformFilter>
}

IPlatformFiltersFactory <|-- PlatformFiltersFactory

interface IPlatformFilter {
match(IPlatform): bool
}
IPlatform <.. IPlatformFilter: <<use>>

class AllFilter
IPlatformFilter <|-- AllFilter
note left
Always return true
end note

class LinuxFilter
IPlatformFilter <|-- LinuxFilter

class AppImageFilter
IPlatformFilter <|-- AppImageFilter

class OsxFilter
IPlatformFilter <|-- OsxFilter

class WindowsFilter
IPlatformFilter <|-- WindowsFilter
}

@enduml
8 changes: 8 additions & 0 deletions lib/MellowPlayer/Infrastructure/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ set(LIB_NAME "${PROJECT_NAME}.Infrastructure")
glob_recurse_excl(SOURCE_FILES *.cpp "Platform")
glob_recurse_excl(HEADER_FILES *.hpp "Platform")

file(GLOB_RECURSE PLATFORM_FILTERS_SOURCES Platform/Filters/*.cpp)
file(GLOB_RECURSE PLATFORM_FILTERS_HEADERS Platform/Filters/*.cpp)
set(SOURCE_FILES ${SOURCE_FILES} ${PLATFORM_FILTERS_SOURCES})
set(HEADER_FILES ${HEADER_FILES} ${PLATFORM_FILTERS_HEADERS})

set(SOURCE_FILES ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/3rdparty/libqxt/src/widgets/qxtglobalshortcut.cpp)
set(SOURCE_FILES ${SOURCE_FILES} Platform/Platform.cpp)
set(HEADER_FILES ${HEADER_FILES} Platform/Platform.hpp)

if (WIN32)
set(SOURCE_FILES ${SOURCE_FILES} ${CMAKE_SOURCE_DIR}/3rdparty/libqxt/src/widgets/win/qxtglobalshortcut_win.cpp)
file(GLOB_RECURSE PLATFORM_SOURCE_FILES Platform/Windows/*.cpp)
Expand Down
59 changes: 59 additions & 0 deletions lib/MellowPlayer/Infrastructure/Platform/Platform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "Platform.hpp"

using namespace MellowPlayer::Infrastructure;


QString Platform::Unsupported = "Unsupported platform";
QString Platform::Linux = "GNU/Linux";
QString Platform::AppImage = "AppImage";
QString Platform::Windows = "Windows";
QString Platform::OSX = "Mac OSX";

QString Platform::name() const
{
QString plaformName = Unsupported;

#ifdef Q_OS_LINUX
QString appImagePath = qgetenv("APPIMAGE");
if (appImagePath.isEmpty())
plaformName = Linux;
else
plaformName = AppImage;
#endif

#ifdef Q_OS_OSX
plaformName = OSX;
#endif

#ifdef Q_OS_WIN
plaformName = Windows;
#endif

return plaformName;
}

bool Platform::match(QList<IPlatformFilter>&) const
{
return false;
}

bool Platform::isLinux() const
{
return name() == Linux;
}

bool Platform::isAppImage() const
{
return name() == AppImage;
}

bool Platform::isWindows() const
{
return name() == Windows;
}

bool Platform::isOsx() const
{
return name() == OSX;
}

46 changes: 46 additions & 0 deletions lib/MellowPlayer/Infrastructure/Platform/Platform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <QList>

namespace MellowPlayer::Infrastructure
{
class IPlatformFilter;

class IPlatform
{
public:
virtual ~IPlatform() = default;

virtual QString name() const = 0;

virtual bool isLinux() const = 0;
virtual bool isAppImage() const = 0;
virtual bool isWindows() const = 0;
virtual bool isOsx() const = 0;

virtual bool match(QList<IPlatformFilter>& filters) const = 0;
};

class Platform: public IPlatform
{
public:
QString name() const override;

bool isLinux() const override;

bool isAppImage() const override;

bool isWindows() const override;

bool isOsx() const override;

bool match(QList<IPlatformFilter>& filters) const override;

private:
static QString Unsupported;
static QString Linux;
static QString AppImage;
static QString Windows;
static QString OSX;
};
}
125 changes: 125 additions & 0 deletions tests/UnitTests/Infrastructure/Platform/PlatformTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <catch.hpp>
#include <MellowPlayer/Infrastructure/Platform/Platform.hpp>

using namespace MellowPlayer::Infrastructure;

SCENARIO("PlatformTests")
{
GIVEN("A platform instance")
{
Platform platform;

WHEN("Call name()")
{
auto name = platform.name();

THEN("name is not empty")
{
REQUIRE(!name.isEmpty());
}
}

#ifdef Q_OS_LINUX
WHEN("on GNU/Linux with no APPIMAGE environment variable defined")
{
THEN("isLinux returns true")
{
REQUIRE(platform.isLinux());
}

THEN("isAppImage returns false")
{
REQUIRE(!platform.isAppImage());
}

THEN("isOsx returns false")
{
REQUIRE(!platform.isOsx());
}

THEN("isWindows returns false")
{
REQUIRE(!platform.isWindows());
}
}

WHEN("on GNU/Linux with APPIMAGE environment variable defined")
{
qputenv("APPIMAGE", "/path/to/appimage");

THEN("isAppImage returns true")
{
REQUIRE(platform.isAppImage());
}

THEN("isLinux returns false")
{
REQUIRE(!platform.isLinux());
}

THEN("isOsx returns false")
{
REQUIRE(!platform.isOsx());
}

THEN("isWindows returns false")
{
REQUIRE(!platform.isWindows());
}

qputenv("APPIMAGE", "");
}
#endif

#ifdef Q_OS_OSX
WHEN("on Mac OSX")
{
THEN("isOsx returns true")
{
REQUIRE(platform.isOsx());
}

THEN("isLinux returns false")
{
REQUIRE(!platform.isLinux());
}

THEN("isAppImage returns false")
{
REQUIRE(!platform.isAppImage());
}

THEN("isWindows returns false")
{
REQUIRE(!platform.isWindows());
}
}
#endif

#ifdef Q_OS_WIN
WHEN("on Windows")
{
THEN("isWindows returns true")
{
REQUIRE(platform.isWindows());
}

THEN("isOsx returns false")
{
REQUIRE(!platform.isOsx());
}

THEN("isLinux returns false")
{
REQUIRE(!platform.isLinux());
}

THEN("isAppImage returns false")
{
REQUIRE(!platform.isAppImage());
}
}
#endif

}
}

0 comments on commit 6c166cf

Please sign in to comment.