Centrafuse Logo Flux Logo


HelloWorld.cpp

See the Getting Started section for a detailed description of this example.

#include "HelloWorld.h"
#include "Setup.h"

using namespace System;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Drawing;
using namespace System::IO;
using namespace System::Windows::Forms;
using namespace System::Xml;
using namespace centrafuse::Plugins;

namespace HelloWorld
{
        HelloWorld::HelloWorld()
        {
                PluginName = "HelloWorld";
                PluginPath = String::Concat("plugins\\", PluginName, "\\");
                PluginPathSkins = String::Concat(PluginPath, "Skins\\");
                PluginPathLanguages = String::Concat(PluginPath, "Languages\\");
                PluginPathIcons = String::Concat(PluginPath, "Icons\\");
                ConfigurationFile = "config.xml";
                ConfigSection = "/APPCONFIG/";
                LanguageSection = "/APPLANG/HELLOWORLD/";
                LogFile= "helloworld.log";
                ImageFolderIndex = 0;
                ImageEmptyIndex = 1;
                LogFilePath = String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\Plugins\\");
                LogFilePath = String::Concat(LogFilePath, PluginName, "\\", LogFile);

        // When set to true all system audio will be paused when the plugin is shown.  The main title,
        // position,and status labels will be cleared and can be populated by the plugin.
                this->CF_pluginPauseAudioFlag = false;
                
        // The plugin name should have a matching section in the skin.xml file.
                this->CF_pluginName = "HELLOWORLD";
                
        // Sets the plugin to be a GUI plugin.  Only GUI plugins are available button actions.
                this->CF_pluginIsGUI = true;

        // Set Settings flags, show in basic settings and advanced settings
        this->CF_pluginHasSettings = true;
        this->CF_pluginHasBasicSettings = true;

        // Create XML Configuration if not exists
        AddConfigXml();
    }

        void HelloWorld::AddConfigXml()
    {
        try
        {
                        String^ configXmlPath = String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\", PluginPath, ConfigurationFile);
            // Create applicationdata folder if doesn't exist
                        if (!Directory::Exists(String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\Plugins\\", PluginName)))
            {
                Directory::CreateDirectory(String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\Plugins\\", PluginName));
            }
            // Create config.xml file if doesn't exist
                        if (!File::Exists(configXmlPath))
            {
                                FileStream^ fs = gcnew FileStream(configXmlPath, FileMode::Create, FileAccess::Write, FileShare::Write);
                fs->Close();
                StreamWriter^ sw = gcnew StreamWriter(configXmlPath, false, System::Text::Encoding::ASCII);
                sw->Write("<APPCONFIG>\r\n    <SKIN>Aura</SKIN>\r\n    <APPLANG>English</APPLANG>\r\n    <LOGEVENTS>False</LOGEVENTS>\r\n</APPCONFIG>\r\n");
                sw->Close();
            }
        }
        catch (Exception^ errmsg) { CFTools::writeError(errmsg->Message, errmsg->StackTrace); }
        return;
    }

        void HelloWorld::WriteLog(String^ msg)
    {
                try 
                {
                        if (Boolean::Parse(this->pluginConfig->readPluginField("/APPCONFIG/LOGEVENTS")))
                                CFTools::writeModuleLog(msg, LogFilePath);
                }
                catch (Exception^) {}
    }

        void HelloWorld::CF_pluginInit()
        {
                try
                {
                        // loads Settings
                        LoadSettings();

                        // Note LoadSettings() must be called before WriteLog() can be used
                        WriteLog("CF_pluginInit");

                        // add event handlers for keyboard and power mode change
                        this->CF_Event_powerModeChanged += gcnew Microsoft::Win32::PowerModeChangedEventHandler(this, &HelloWorld::HelloWorld_CF_Event_powerModeChanged);
                        this->KeyDown += gcnew KeyEventHandler(this, &HelloWorld::HelloWorld_KeyDown);
                }
                catch(Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
        }

        void HelloWorld::CF_localskinsetup()
        {
        WriteLog("CF_localskinsetup");
                try
                {
                        // Clears all control arrays.  Should always be in CF_localskinsetup so the plugin will properly
                        // reset when the resolution changes.
                        this->CF_clearControls();
                        
            // Initializes the skin with the specified skin file path, if it exists.
                        // Otherwise load the default skin.
                        String^ path = String::Concat(PluginPathSkins, this->currentSkin);
                        this->CF_initSkin(String::Concat(path, "\\skin.xml"));
                        
            // Loads the skin images.
                        this->CF_loadImages(String::Concat(path, "\\HelloWorld_off.png"), String::Concat(path, "\\HelloWorld_down.png"));
                        ScalePlugin();
                
                        // create labels 
                        this->CF_createLabel("LBLHEADER");
                        this->CF_updateText("LBLHEADER", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/LBLHEADER"));
                        this->CF_createLabel("LBLTEXT");
                        this->CF_updateText("LBLTEXT", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/LBLTEXT1"));
                        
            // create buttons
                        this->CF_createButton("BTN1", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/BTN1"), gcnew MouseEventHandler(this, &HelloWorld::BTN1_Click));
                        this->CF_createButton("BTN2", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/BTN2"), gcnew MouseEventHandler(this, &HelloWorld::BTN2_Click));
            this->CF_createButton("BTN3", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/BTN3"), gcnew MouseEventHandler(this, &HelloWorld::BTN3_Click));
            this->CF_createButton("BTN4", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/BTN4"), gcnew MouseEventHandler(this, &HelloWorld::BTN4_Click));
            this->CF_createButton("BTN5", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/BTN5"), gcnew MouseEventHandler(this, &HelloWorld::BTN5_Click));
            this->CF_createButton("BTN6", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/BTN6"), gcnew MouseEventHandler(this, &HelloWorld::BTN6_Click));       
                }
                catch(Exception^ errmsg)
                {
                        CFTools::writeError(errmsg->ToString());
                }
        }

        void HelloWorld::CF_pluginClose()
        {
        WriteLog("CF_pluginClose");
                delete(this);
        }
        
        void HelloWorld::CF_pluginShow()
        {
        WriteLog("CF_pluginShow");
                // Shows the plugin
                this->Visible = true;

        // If your plugin has a listbos, it's a good idea to give the focus to it when
        // the plugin shows up. To do so, remove the comment and replace the $LISTBOX-NAME$
        // with the name of your listbox.
        //$LISTBOX-NAME$.Focus();
        }

        void HelloWorld::CF_pluginUpdateLanguageSkin(String^ lang, String^ skin)
        {
        WriteLog("CF_pluginUpdateLanguageSkin");
                this->language = lang;

                // Update plugin config with global cf language
                if (File::Exists(String::Concat(PluginPathLanguages, language, ".xml")))
        {
            XmlDocument^ configxml = gcnew XmlDocument();
            configxml->Load(String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\", PluginPath, ConfigurationFile));
            configxml->SelectSingleNode("/APPCONFIG/APPLANG")->InnerText = System::Web::HttpUtility::HtmlEncode(language);
            configxml->Save(String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\", PluginPath, ConfigurationFile));
        }

        // Update plugin config with global cf skin
                if (Directory::Exists(String::Concat(PluginPathSkins, skin)))
        {
            XmlDocument^ configxml = gcnew XmlDocument();
            configxml->Load(String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData),"\\Centrafuse\\",PluginPath, ConfigurationFile));
                        configxml->SelectSingleNode("/APPCONFIG/SKIN")->InnerText = System::Web::HttpUtility::HtmlEncode(skin);
            configxml->Save(String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\", PluginPath, ConfigurationFile));
        }

                LoadSettings();
        }

        DialogResult HelloWorld::CF_pluginShowSetup()
        {
        WriteLog("CF_pluginShowSetup");
                
        // Return DialogResult.OK for the main application to update from plugin changes.
                Windows::Forms::DialogResult returnvalue = Windows::Forms::DialogResult::Cancel;

                try
                {
                        // Creates a new plugin setup instance. If you create a CFDialog or CFSetup you must
                        // set its MainForm property to the main plugins MainForm property.
                        Setup^ setup = gcnew Setup();
                        setup->MainForm = this->MainForm;
                        setup->setupSection();
                        returnvalue = setup->ShowDialog();
                        if(returnvalue == Windows::Forms::DialogResult::OK)
                        {
                // add code here for saving the modified settings

                                // Reloads plugin configuration.
                                LoadSettings();
                        }
                        setup->Close();
                        setup = nullptr;
                }
                catch(Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }

                return returnvalue;
        }

        void HelloWorld::CF_pluginPause()
        {
        WriteLog("CF_pluginPause");
        }

        void HelloWorld::CF_pluginResume()
        {
        WriteLog("CF_pluginResume");
        }

        void HelloWorld::CF_pluginCommand(String^ command, String^ param1, String^ param2)
        {
        WriteLog(String::Concat("HelloWorld: CF_pluginCommand: ", command, " ", param1, ", ", param2));
        }

        String^ HelloWorld::CF_pluginData(String^ command, String^ param)
        {
                WriteLog(String::Concat("HelloWorld: CF_pluginData: " + command + " " + param));
                String^ returnValue = "";
                return returnValue;
        }

        void HelloWorld::LoadSettings()
        {
        String^ configfilepath = String::Concat(Environment::GetFolderPath(Environment::SpecialFolder::LocalApplicationData), "\\Centrafuse\\", PluginPath, ConfigurationFile);

                // Loads the plugin configuration from the specified path. 
        this->CF_loadConfig(configfilepath);

                // Call writeModuleLog() with the string startup() to keep only last 2 runtimes...
                // Note CF_loadConfig() must be called before WriteLog() can be used
                WriteLog("startup");
                WriteLog("Loaded settings");

        // Check if the skin matches the main application.  If it does not match and it does exist then change
        // the skin to matcht he main application.
                if (this->CF_getConfigSetting(CF_ConfigSettings::Skin)->Trim()->ToUpper() != this->pluginConfig->readPluginField("/APPCONFIG/SKIN")->Trim()->ToUpper())
        {
                        if (Directory::Exists(String::Concat(PluginPathSkins, this->CF_getConfigSetting(CF_ConfigSettings::Skin))))
            {
                XmlDocument^ configxml = gcnew XmlDocument();
                configxml->Load(configfilepath);
                                configxml->SelectSingleNode("/APPCONFIG/SKIN")->InnerText = System::Web::HttpUtility::HtmlEncode(this->CF_getConfigSetting(CF_ConfigSettings::Skin));
                configxml->Save(configfilepath);
            }
                        else if (!Directory::Exists(String::Concat(PluginPathSkins, this->pluginConfig->readPluginField("/APPCONFIG/SKIN"))))
            {
                XmlDocument^ configxml = gcnew XmlDocument();
                configxml->Load(configfilepath);
                configxml->SelectSingleNode("/APPCONFIG/SKIN")->InnerText = System::Web::HttpUtility::HtmlEncode("Onyx WS Night");
                configxml->Save(configfilepath);
            }

            this->CF_loadConfig(configfilepath);
        }
                else if (!Directory::Exists(String::Concat(PluginPathSkins, this->pluginConfig->readPluginField("/APPCONFIG/SKIN"))))
        {
            XmlDocument^ configxml = gcnew XmlDocument();
            configxml->Load(configfilepath);
            configxml->SelectSingleNode("/APPCONFIG/SKIN")->InnerText = System::Web::HttpUtility::HtmlEncode("Onyx WS Night");
            configxml->Save(configfilepath);

            this->CF_loadConfig(configfilepath);
        }

        this->currentSkin = this->pluginConfig->readPluginField("/APPCONFIG/SKIN");

        // Loads the plugin language file from the specified path. This is loaded using the language specified 
        // in the configuration file.
                if (this->CF_getConfigSetting(CF_ConfigSettings::Language)->Trim()->ToUpper() != this->pluginConfig->readPluginField("/APPCONFIG/APPLANG")->Trim()->ToUpper())
        {
                        if (File::Exists(String::Concat(PluginPathLanguages, this->CF_getConfigSetting(CF_ConfigSettings::Language), ".xml")))
            {
                XmlDocument^ configxml = gcnew XmlDocument();
                configxml->Load(configfilepath);
                                configxml->SelectSingleNode("/APPCONFIG/APPLANG")->InnerText = System::Web::HttpUtility::HtmlEncode(this->CF_getConfigSetting(CF_ConfigSettings::Language));
                configxml->Save(configfilepath);
            }
                        else if (!File::Exists(String::Concat(PluginPathLanguages, this->pluginConfig->readPluginField("/APPCONFIG/APPLANG"), ".xml")))
            {
                XmlDocument^ configxml = gcnew XmlDocument();
                configxml->Load(configfilepath);
                configxml->SelectSingleNode("/APPCONFIG/APPLANG")->InnerText = System::Web::HttpUtility::HtmlEncode("English");
                configxml->Save(configfilepath);
            }

            this->CF_loadConfig(configfilepath);
        }
                else if (!File::Exists(String::Concat(PluginPathLanguages, this->pluginConfig->readPluginField("/APPCONFIG/APPLANG"), ".xml")))
        {
            XmlDocument^ configxml = gcnew XmlDocument();
            configxml->Load(configfilepath);
            configxml->SelectSingleNode("/APPCONFIG/APPLANG")->InnerText = System::Web::HttpUtility::HtmlEncode("English");
            configxml->Save(configfilepath);

            this->CF_loadConfig(configfilepath);
        }

        this->CF_loadLang(String::Concat(PluginPathLanguages, this->pluginConfig->readPluginField("/APPCONFIG/APPLANG"), ".xml"));

                // The display name is shown in the application to represent
                // the plugin.  This sets the display name from the configuration file.
                this->CF_pluginDisplayName = TranslateText("DISPLAYNAME");

        // All controls should be created or Setup in CF_localskinsetup.
        // This method is also called when the resolution or skin has changed.
        this->CF_localskinsetup();
        }

        String^ HelloWorld::TranslateText(String^ text)
        {
                String^ langread = this->pluginLang->readPluginField(String::Concat(LanguageSection, text));

                if(langread == "")
                        return text;
                else
                        return langread->Replace("\\n", "\n");
        }

        void HelloWorld::ScalePlugin()
        {
        WriteLog("ScalePlugin");
                try
                {
                        this->Bounds.X =  ( ( Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::Width ) ) -
                                ( ( int ) ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/WIDTH") ) )* this->pluginSkinReader->skinWidthRatio) ) )/
                                2 ) + Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::XPosition ) ) +
                                Screen::AllScreens[ this->pluginDisplay - 1 ]->Bounds.X;

                        this->Bounds.Y = ( ( Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::Height ) ) -
                                ( ( int )
                                ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/HEIGHT") ) )* this->pluginSkinReader->skinHeightRatio ) ) )/2 ) +
                                Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::YPosition ) ) +
                                Screen::AllScreens[ this->pluginDisplay - 1 ]->Bounds.Y;

                        this->Bounds.Width = ( ( int ) ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/WIDTH") ) )* this->pluginSkinReader->skinWidthRatio ) );

                        this->Bounds.Height = ( ( int ) ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/HEIGHT") ) )* this->pluginSkinReader->skinHeightRatio ) );
                        
                        /*this->Bounds =
                                gcnew Rectangle(
                                ( ( Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::Width ) ) -
                                ( ( int ) ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/WIDTH") ) )* this->pluginSkinReader->skinWidthRatio) ) )/
                                2 ) + Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::XPosition ) ) +
                                Screen::AllScreens[ this->pluginDisplay - 1 ]->Bounds.X,
                                ( ( Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::Height ) ) -
                                ( ( int )
                                ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/HEIGHT") ) )* this->pluginSkinReader->skinHeightRatio ) ) )/2 ) +
                                Int32::Parse( this->CF_getConfigSetting( CF_ConfigSettings::YPosition ) ) +
                                Screen::AllScreens[ this->pluginDisplay - 1 ]->Bounds.Y,
                                ( ( int ) ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/WIDTH") ) )* this->pluginSkinReader->skinWidthRatio ) ),
                                ( ( int ) ( Int32::Parse( this->pluginSkinReader->readPluginField( String::Concat(screenpath, "/HEIGHT") ) )* this->pluginSkinReader->skinHeightRatio ) ) );
                                */
                }
                catch(Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
        }

    void HelloWorld::BTN1_Click(Object^ sender, MouseEventArgs^ e)
    {
        // The Button #1 has been clicked...
        WriteLog("BTN1 clicked.");
        try
        {
            // Update the text of the bottom on-screen label
            this->CF_updateText("LBLTEXT", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/LBLTEXT1"));
        }
        catch (Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
    }

    void HelloWorld::BTN2_Click(Object^ sender, MouseEventArgs^ e)
    {
        // The Button #2 has been clicked...
        WriteLog("BTN2 clicked.");
        try
        {
            // Update the text of the bottom on-screen label
            this->CF_updateText("LBLTEXT", this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/LBLTEXT2"));
        }
        catch (Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
    }

    void HelloWorld::BTN3_Click(Object^ sender, MouseEventArgs^ e)
    {
        // The Button #3 has been clicked...
        WriteLog("BTN3 clicked.");
        try
        {
            String^ song = "";
            // Emit CF action to retrieve the name of the current song playing
                        song = CF_systemGetText(CF_TextItems::CurrentTitle);
            // You could also retrieve artist, album, etc...
            // CF_getText(CF_TextItems.CurrentArtist) 
            // CF_getText(CF_TextItems.CurrentAlbum)
            // Update the text of the bottom on-screen label
            // Update the text of the bottom on-screen label
            this->CF_updateText("LBLTEXT", song);

        }
        catch (Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
    }

    void HelloWorld::BTN4_Click(Object^ sender, MouseEventArgs^ e)
    {
        // The Button #4 has been clicked...
        WriteLog("BTN4 clicked.");
        try
        {
            // Display an ok box that will make the user confirm a message
            this->CF_systemDisplayDialog(CF_Dialogs::OkBox, "This is an OK box");
        }
        catch (Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
    }

    void HelloWorld::BTN5_Click(Object^ sender, MouseEventArgs^ e)
    {
        // The Button #5 has been clicked...
        WriteLog("BTN5 clicked.");
        try
        {
            // Display a status box that will autohide
                        this->CF_systemCommand(CF_Actions::SHOWINFO, "Status Info", "AUTOHIDE");
        }
        catch (Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
    }

    void HelloWorld::BTN6_Click(Object^ sender, MouseEventArgs^ e)
    {
        // The Button #6 has been clicked...
        WriteLog("BTN6 clicked.");
        try
        {
            // Update the text to what user enters in osk
            Object^ tempobject;
            String^ resultvalue, ^resulttext;
                        if (this->CF_systemDisplayDialog(CF_Dialogs::OSK, this->pluginLang->readPluginField("/APPLANG/HELLOWORLD/ENTERTEXT"), "", nullptr, resultvalue, resulttext, tempobject, nullptr, true, true, true, true, false, false, 1) == System::Windows::Forms::DialogResult::OK)
            {
                this->CF_updateText("LBLTEXT", resultvalue);
            }
        }
        catch (Exception^ errmsg) { CFTools::writeError(errmsg->ToString()); }
    }

        void HelloWorld::HelloWorld_CF_Event_powerModeChanged(Object^ sender, Microsoft::Win32::PowerModeChangedEventArgs^ e)
        {

        }

    // If the plugin uses back/forward buttons, we need to catch the left/right keyboard commands too...
        void HelloWorld::HelloWorld_KeyDown(Object^ sender, KeyEventArgs^ e)
        {
                e->Handled = true;

                if(e->KeyCode == Keys::Left)
                {
                        //---------------------------------------------------------------------------
                        // TODO: replace this if needed
                        //--------------------------------------------------------------------------- 
                        //this->back_Click(this, new MouseEventArgs(MouseButtons.Left,1,0,0,0));
                }
                else if(e->KeyCode == Keys::Right)
                {
                        //---------------------------------------------------------------------------
                        // TODO: replace this if needed
                        //--------------------------------------------------------------------------- 
                        //this->forward_Click(this, new MouseEventArgs(MouseButtons.Left,1,0,0,0));
                }
        }

}




Copyright © 2008 Flux Media, Inc. (U.S. Copyright Registration Number: TXu-1-239-794) Flux Logo