Skip to content

Commit

Permalink
separated the functions and class stuff into .h/.cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
svenburkard committed Sep 8, 2014
1 parent 8d038e1 commit 89b93f2
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 130 deletions.
23 changes: 13 additions & 10 deletions xbmc/ApplicationMessenger.cpp
Expand Up @@ -226,42 +226,45 @@ void CApplicationMessenger::ProcessMessages()

void CApplicationMessenger::ProcessMessage(ThreadMessage *pMsg)
{

CshutdownHookCheck CshutdownHookCheck;

switch (pMsg->dwMessage)
{
case TMSG_SHUTDOWN:
{
switch (CSettings::Get().GetInt("powermanagement.shutdownstate"))
{
case POWERSTATE_SHUTDOWN:
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
Powerdown();
break;
}

case POWERSTATE_SUSPEND:
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
Suspend();
break;
}

case POWERSTATE_HIBERNATE:
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
Hibernate();
break;
}

case POWERSTATE_QUIT:
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
Quit();
break;
}

case POWERSTATE_MINIMIZE:
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
Minimize();
break;
Expand All @@ -276,7 +279,7 @@ void CApplicationMessenger::ProcessMessage(ThreadMessage *pMsg)

case TMSG_POWERDOWN:
{
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
g_application.Stop(EXITCODE_POWERDOWN);
g_powerManager.Powerdown();
Expand All @@ -286,7 +289,7 @@ void CApplicationMessenger::ProcessMessage(ThreadMessage *pMsg)

case TMSG_QUIT:
{
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
g_application.Stop(EXITCODE_QUIT);
}
Expand All @@ -295,7 +298,7 @@ void CApplicationMessenger::ProcessMessage(ThreadMessage *pMsg)

case TMSG_HIBERNATE:
{
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
g_PVRManager.SetWakeupCommand();
g_powerManager.Hibernate();
Expand All @@ -305,7 +308,7 @@ void CApplicationMessenger::ProcessMessage(ThreadMessage *pMsg)

case TMSG_SUSPEND:
{
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
g_PVRManager.SetWakeupCommand();
g_powerManager.Suspend();
Expand All @@ -316,7 +319,7 @@ void CApplicationMessenger::ProcessMessage(ThreadMessage *pMsg)
case TMSG_RESTART:
case TMSG_RESET:
{
if(shutdownHookCheck())
if(CshutdownHookCheck.shutdownHookCheck())
{
g_application.Stop(EXITCODE_REBOOT);
g_powerManager.Reboot();
Expand Down
1 change: 1 addition & 0 deletions xbmc/Makefile.in
Expand Up @@ -22,6 +22,7 @@ SRCS=Application.cpp \
PlayListPlayer.cpp \
PartyModeManager.cpp \
SectionLoader.cpp \
shutdownHookCheck.cpp \
SystemGlobals.cpp \
Temperature.cpp \
TextureCache.cpp \
Expand Down
133 changes: 133 additions & 0 deletions xbmc/shutdownHookCheck.cpp
@@ -0,0 +1,133 @@
// shutdownHookCheck.cpp
//
// @AUTHOR: Sven Burkard <dev@sven-burkard.de>
// @DESC..: checks for user defined xbmc-shutdown-hooks in a predefined hook directory.
// @DESC..: if one of the hook scripts returns an exit code not 0,
// @DESC..: the xbmc shutdown will be stopped and the user will be informed via xbmc-notifications
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// system includes
#include <cstdlib> // system / getenv
//#include <iostream> // cout
#include <dirent.h> // dir...
#include <string.h> // strcmp
#include <sys/stat.h> // stat

// xbmc includes
#include "utils/log.h"
#include "dialogs/GUIDialogKaiToast.h"
#include "filesystem/SpecialProtocol.h"
#include "shutdownHookCheck.h"


using namespace std;


////////////////////////////////////////////////////////
bool fileIsExecutable(const string file)
{
////////////////////////////////////////////////////////

struct stat st;

if(stat(file.c_str(), &st) < 0)
{
return false;
}
else if((st.st_mode & S_IEXEC) != 0)
{
return true;
}

return false;
}


////////////////////////////////////////////////////////
bool CshutdownHookCheck::shutdownHookCheck()
{
////////////////////////////////////////////////////////

DIR* dir;
dirent* pdir;
int shutdownStopped = 0;
string dirHome;
string dirHooks;

/*
//////////////////////////////////////////////////
create a path-variable of the hook scripts dir
//////////////////////////////////////////////////
*/
dirHome = CSpecialProtocol::TranslatePath("special://home/").c_str();

CLog::Log(LOGDEBUG,"[shutdownHookCheck] dirHome:>>%s<<", dirHome.c_str());


dirHooks += dirHome;
dirHooks += "../xbmcShutdownHooks/hooks-enabled/";

CLog::Log(LOGDEBUG,"[shutdownHookCheck] dirHooks:>>%s<<", dirHooks.c_str());
/*
//////////////////////////////////////////////////
*/

dir = opendir(dirHooks.c_str());

if(!dir)
{
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, "shutdown-hook: " + dirHooks, "hooks directory couldn't be opened, shutdown anyway");
CLog::Log(LOGWARNING,"[shutdownHookCheck] hooks directory (dirHooks: %s) couldn't be opened, shutdown anyway", dirHooks.c_str());
return true; // return true because: a missing shutdown hook dir should't be a reason to abort exit/shutdown
}

while((pdir = readdir(dir)) != NULL)
{

CLog::Log(LOGDEBUG,"[shutdownHookCheck] shutdownStopped:>>%i<<", shutdownStopped);

if(!strcmp(pdir->d_name, ".")) continue;
if(!strcmp(pdir->d_name, "..")) continue;
if(!strcmp(pdir->d_name, ".gitignore")) continue;
if(!strcmp(pdir->d_name, "README")) continue;
if(shutdownStopped == 1) continue;


string scriptName = pdir->d_name;
string combinedCmd = dirHooks+scriptName;

CLog::Log(LOGDEBUG,"[shutdownHookCheck] scriptName:>>%s<<", scriptName.c_str());

if(!fileIsExecutable(combinedCmd))
{
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, "shutdown-hook-check: ", scriptName + ": can NOT be executed");
CLog::Log(LOGERROR,"[shutdownHookCheck] %s : can NOT be executed", scriptName.c_str());
return false;
}


int exitCode = system(combinedCmd.c_str());

if(exitCode == 0)
{
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, "shutdown-hook-check: ", scriptName + ": ok");
CLog::Log(LOGINFO,"[shutdownHookCheck] %s: ok", scriptName.c_str());
}
else
{
shutdownStopped = 1;
CLog::Log(LOGDEBUG,"[shutdownHookCheck] setting shutdownStopped to 1 !!!");

CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, "shutdown-hook-check: ", "shutdown stopped by " + scriptName);
CLog::Log(LOGINFO,"[shutdownHookCheck] shutdown stopped by %s", scriptName.c_str());

closedir(dir);
return false;
}

}
closedir(dir);


return true;
}
124 changes: 4 additions & 120 deletions xbmc/shutdownHookCheck.h
Expand Up @@ -6,126 +6,10 @@
// @DESC..: the xbmc shutdown will be stopped and the user will be informed via xbmc-notifications
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// system includes
#include <cstdlib> // system / getenv
//#include <iostream> // cout
#include <dirent.h> // dir...
#include <string.h> // strcmp
#include <sys/stat.h> // stat

// xbmc includes
#include "dialogs/GUIDialogKaiToast.h"
#include "filesystem/SpecialProtocol.h"


using namespace std;


////////////////////////////////////////////////////////
bool fileIsExecutable(const string file)
{
////////////////////////////////////////////////////////

struct stat st;

if(stat(file.c_str(), &st) < 0)
{
return false;
}
else if((st.st_mode & S_IEXEC) != 0)
{
return true;
}

return false;
}


////////////////////////////////////////////////////////
bool shutdownHookCheck()
class CshutdownHookCheck
{
////////////////////////////////////////////////////////

DIR* dir;
dirent* pdir;
int shutdownStopped = 0;
string dirHome;
string dirHooks;

/*
//////////////////////////////////////////////////
create a path-variable of the hook scripts dir
//////////////////////////////////////////////////
*/
dirHome = CSpecialProtocol::TranslatePath("special://home/").c_str();

CLog::Log(LOGDEBUG,"[shutdownHookCheck] dirHome:>>%s<<", dirHome.c_str());


dirHooks += dirHome;
dirHooks += "../xbmcShutdownHooks/hooks-enabled/";

CLog::Log(LOGDEBUG,"[shutdownHookCheck] dirHooks:>>%s<<", dirHooks.c_str());
/*
//////////////////////////////////////////////////
*/

dir = opendir(dirHooks.c_str());

if(!dir)
{
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Warning, "shutdown-hook: " + dirHooks, "hooks directory couldn't be opened, shutdown anyway");
CLog::Log(LOGWARNING,"[shutdownHookCheck] hooks directory (dirHooks: %s) couldn't be opened, shutdown anyway", dirHooks.c_str());
return true; // return true because: a missing shutdown hook dir should't be a reason to abort exit/shutdown
}

while((pdir = readdir(dir)) != NULL)
{

CLog::Log(LOGDEBUG,"[shutdownHookCheck] shutdownStopped:>>%i<<", shutdownStopped);

if(!strcmp(pdir->d_name, ".")) continue;
if(!strcmp(pdir->d_name, "..")) continue;
if(!strcmp(pdir->d_name, ".gitignore")) continue;
if(!strcmp(pdir->d_name, "README")) continue;
if(shutdownStopped == 1) continue;


string scriptName = pdir->d_name;
string combinedCmd = dirHooks+scriptName;

CLog::Log(LOGDEBUG,"[shutdownHookCheck] scriptName:>>%s<<", scriptName.c_str());

if(!fileIsExecutable(combinedCmd))
{
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Error, "shutdown-hook-check: ", scriptName + ": can NOT be executed");
CLog::Log(LOGERROR,"[shutdownHookCheck] %s : can NOT be executed", scriptName.c_str());
return false;
}


int exitCode = system(combinedCmd.c_str());

if(exitCode == 0)
{
CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, "shutdown-hook-check: ", scriptName + ": ok");
CLog::Log(LOGINFO,"[shutdownHookCheck] %s: ok", scriptName.c_str());
}
else
{
shutdownStopped = 1;
CLog::Log(LOGDEBUG,"[shutdownHookCheck] setting shutdownStopped to 1 !!!");

CGUIDialogKaiToast::QueueNotification(CGUIDialogKaiToast::Info, "shutdown-hook-check: ", "shutdown stopped by " + scriptName);
CLog::Log(LOGINFO,"[shutdownHookCheck] shutdown stopped by %s", scriptName.c_str());

closedir(dir);
return false;
}

}
closedir(dir);

public:
bool shutdownHookCheck();
};

return true;
}

0 comments on commit 89b93f2

Please sign in to comment.