Plugin: Shutdown Manager (or call it whatever you want ;) ) - A blog to develop a MP2 plugin

Discussion in 'Plugin Development' started by chefkoch, December 14, 2012.

  1. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    Hi,

    I am missing an essential feature in MP2: I want to choose different power actions from a menu to shutdown the pc, supend or restart it. Close and Minimize MP2 etc.

    I already wrote down some ideas and specification in wiki: http://wiki.team-mediaportal.com/index.php?title=2_MEDIAPORTAL_2/Contribute/Development/Tasks_&_Todos/Shutdown_menu

    The branch which will be used for the development can be found on GIT: https://github.com/MediaPortal/MediaPortal-2/tree/FEAT_ShutdownManager

    After some time now, I decided to try to develop it on my own. There are a few different things which needs to be implemented:
    1. The name of the plugin ;) Does not matter at this stage, and will be re-defined during the code review after everything else is done.
    2. The GUI dialog for showing the user the shutdown / power actions which will be executed immediately.
    3. The GUI dialog for showing the user the shutdown / power actions which will be executed delayed. (Sleeptimer feature)
    4. A settings UI to:
      - reorder items of dialog (2.) containing the immediate executed actions
      - hide/show item of dialog (2.) containing the immediate executed actions
    5. The implementation to actually perform the power actions.
    6. Make the dialogs or the power actions available in GUI and via keybindings.
    I will post more ideas, progress updates, problems and their fixes within this thread.

    Please remember that these are the steps I worked myself into the topic. It is not best practice. If there are things which I improve later I try to link to the better practice from the worse one.



    Table of Contents:
    Show Spoiler

    • Conclusions & ToDo tasks in general (independent from this plugin/implementation):
    • Documentation of the default available styles/ controls....
    • Documentation of the available Commands/ properties?
    • Where does DataContext apply??? i.e.
      <ControlTemplate x:Key="Contents_Template" DataContext="{Binding Source={StaticResource Model}}">
      on ControlTemplate does not work, but
      <StackPanel DataContext="{Binding Source={StaticResource Model}}">
      on StackPanel does.
    • How to build and use ItemLists? Which data type are the items of? derived from ListItem? ...
    • What is ListItem AdditionalProperties for?
    • How to find defintions of styles? where to look for, i.e. EditPlaylistListViewStyle
     
    Last edited: December 30, 2012
    • Like Like x 3
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    At the beginning the project for the plugin needs to be set up in the MP2-Client.sln.

    The basic directory structure has been created using:
    • ShutdownManager\plugin.xml: contains all required plugin definitions.
    • ShutdownManager\Language: contains the english language file. Translations to other languages will be done at Transifex, but won't be added to that development-folder.
    • ShutdownManager\Models: contains the code for the dialogs, in other words there we will define the information which are available for the skins.
    • ShutdownManager\Skin\default: contains the skin files for the dialogs. We only need to provide these default files and the plugin can be used immediately by all other skins, without the need to write or copy any skin files to those.
    Let's start with the plugin.xml.
    This registers our models to MP2 so that skins can access their public properties or execute public methods, or the models could be referenced by workflow states. (follows next)

    This creates 2 workflow states as dialogs. Here the dialogs are referencing to the models, which we defined before.

    To make the dialogs accessible from the skin, I added 2 new menu entries to the home menu for the beginning, by adding a skin file ShutdownManager/Skin/default/workflow/shutdown-actions.xml.
    More information a detailed discription of workflows, models, states etc can be found in wiki.

    The result so far is:
    menuitem.png
     
    Last edited: December 14, 2012
    • Like Like x 1
  4. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    Now that we defined the DialogStates dialogShutdown and dialogShutdownTimer the appropriate files in ShutdownManager/Skin/default/screens needs to be added.

    The first dialog model is created as ShutdownManager/Models/ShutdownDialogModel.cs. For testing purposes the executed shutdown action is only written to log files instead of i.e. shutting down the PC for real.
    Almost every public method of each available model in MP2 can be referenced and executed as a command by almost every screen. In ShutdownManager/Skin/default/screens/dialogShutdown.xaml only the ShutdownDialogModel is referenced through it's Id
    The model can be referenced inside this screen file now by the Key "Model". So a Key is kind of a placeholder.

    The dialog is inherited from screens\master_dialog_bare.xaml through:
    Additional to Close button which is already available because of master_dialog_bare.xaml, each shutdown action is added as a button to the dialog. For grouping a StackPanelis used. The datacontext for the StackPanel is set to Model which is 25F16911-ED0D-4439-9858-5E69C970C037 in the end.

    dialog.png

    Clicking on these buttons writes the action to the log:
    log.png
     
    Last edited: December 14, 2012
    • Like Like x 1
  5. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    Use constants for references in code to
    • workflow states,
    • localization resource identifier and
    • GUI keys
    I added a new class "General\Consts.cs". There all kind of constant values, especially strings, are being defined. These constants can be reused directly within all code of the ShutdownManager project.

    The workflow states are defined in the plugin.xml file. To reuse them in code directly they are added to the Consts class. The ID is defined as text and redefined as GUID to be reused in code immediately.
    Workflow states in plugin\Consts.cs
    Workflow states in plugin.xml
    You might remember that we also defined the Workflow model IDs within the plugin.xml. A definition inside Consts.cs is not needed, because these Model IDs are defined in the models directly as a constant. Examples:
    ShutdownDialogModel (string GUID), ShutdownTimerDialogModel (string GUID), ShutdownConfigurationDialogModel (string GUID)

    The localization resource identifiers (LRI) can be reused in code to fill properties and list items etc with localized code.
    In the example I use another helper method for setting the list item text depending on the ShutdownAction.
    The LRI in Consts can refer to those which are in the plugin's own language file or even to those which are provided by others.
    Localization resource identifies in plugin\Consts.cs
    Localization resource identifies in strings_en.xml
    The accessor keys for GUI communication in

    Accessor keys in General\Consts.cs
    Usage of accessor keys in plugin code
    Usage of accessor keys in skin files
    More infos about filling an itemlist from code and using it within skin as well as infos about settings handling will follow.
     
    Last edited: December 19, 2012
    • Like Like x 1
  6. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    Settings and Configuration Management in MediaPortal 2
    Please also read wiki for more information about settings, configuration and their difference.

    Loading/Saving settings from/to xml files is done by the SettingsManager.
    A plugin only needs to provide it's own class and all's properties are then automatically (de-)serialized from/to the xml files. The plugin itself does not need to care about any Serialization or file handling stuff.

    The ShutdownSettings contains three public properties:
    For each setting
    • the scope, whether it is a MP2 global or a 'per-user' setting and
    • a default value will be defined.
    Loading the settingsis simply done by:
    After settings are loaded, all it's properties can be accessed as usual.

    Saving the settings is similar to loading them:
    Up to this point it is already possible to load/save various settings from/to xml files, without writing any line of code regarding paths, file names, or xml file handling.

    Even if the settings management is already working, nothing has been changed from the user point of view. To change settings the configuration management is needed. Like explained in the first post, the user should be able to configure the order and visibility of the shutdown items.
    To achieve this a CustomConfigSetting is needed. For using other settings, like YesNo (Boolean), paths, Single- or MutlipleSelections please take a look at the wiki and other existing plugins.
    Show Spoiler
    Example: Videoplayer\DefaultGeometry: registration of ConfigSetting in plugin.xml with reference to DefaultGeometry class, definition of DefaultGeometry class as SingleSelectionSetting, DefaultGeometry in PlayerSettings

    Compared i.e. to the DefaultGeometry setting, which is only a SingleSelection out of a list and already has the overloads Load() and Save(), is our CustomConfigSetting ShutdownDialogSetting empty.
    Within the plugin.xml
    • a setting ShutdownDialogSetting is being registered as CustomConfigSetting with reference to the dialogShutdownConfiguration workflow state
    Creating the GUI for the CustomConfigSetting
    At next a new class ShutdownItem with it's properties Action (type: ShutdownAction) and Enabled (type: bool) has been implemented.
    The ShutdownSettings class got the property ShutdownItemsList (type: List<ShutdownItems>)
    Now it is already possible to save and load the order and the visibility of the shutdown items.

    Let's make it available to GUI through the new ShutdownConfigurationModel.
    It loads and saves the list of shutdown items and provides public properties and methods for usage within the GUI.
    Within skin skin files it is possible to trigger:
    Additionally an ItemList of available shutdown actions is provided. Each item of that ItemList has AddtionalProperties. The currently the important one here is: IsChecked(in code the constant KEY_IS_CHECKED is being used, see previous post for infos about the constans and their usage)
    Let's design the screen files (skin file) for the dialogShutdownConfiguration:
    The Layout could be improved and slimmed down in future. If I'll do that I'll make a separated post and link to it from here.
    What we've got here:
    51-config.png

    As you can see you can freely define any screen like you want it to in MP2. You even could build the list horizontal instead of vertical and move everything from left to right etc, without any changes to plugin code itself, because the required properties and methods are already populated.

    Screens of the other dialogs:
    52-menu.png 53-timer.png

    The other properties of the ShutdownSettings are not populated to the user, but managed by the ShutdownTimerModel: Load, Save

    Next task should be the collection of infos about the remaining playtime of the current media and the current active playlist, their conditional visibility in dialogShutdownTimer and some reworks, cleanups & imrovements in code and skin files.
     
    Last edited: December 30, 2012
    • Like Like x 1
  7. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    Kind of a TOC has been added to first post.
    Some dialog concepts have been added to wiki.
    Some first basic explanations have been added to the second post.
    Executing commands from the skin file has been described in third post.
     
    Last edited: December 14, 2012
    • Like Like x 3
  8. MJGraf
    • Premium Supporter

    MJGraf Retired Team Member

    Joined:
    January 13, 2006
    Messages:
    2,475
    Likes Received:
    796
    Ratings:
    +1,372 / 1
    This is great - keep it going!!! (y)
     
    • Like Like x 1
  9. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    infos about using a class for constants has been added.

    More to come...
     
    • Like Like x 3
  10. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    • Like Like x 3
  11. chefkoch
    • Premium Supporter

    chefkoch Retired Team Member

    Joined:
    October 5, 2004
    Messages:
    3,130
    Likes Received:
    1,456
    Gender:
    Male
    Location:
    Dresden / Munich / Maastricht
    Ratings:
    +1,773 / 1
    Home Country:
    Germany Germany
    Ok to finish this thread and add latest information:

    The ShutdownManager has been implemented as a plugin called: "SystemStateMenu"
    For documentation take a look at the wiki page: http://wiki.team-mediaportal.com/2_MEDIAPORTAL_2/Reference/SystemStateMenu
    There are also links included to the source code and other related components.

    The "sleep timer" feature is not included within "SystemStateMenu", yet.
    For documentation of it's design concept take a look at the it's wiki page: http://wiki.team-mediaportal.com/2_MEDIAPORTAL_2/Contribute/zz_Design_Concepts/Sleeptimer
     
    • Like Like x 3
Loading...

Users Viewing Thread (Users: 0, Guests: 0)

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice
  • About The Project

    The vision of the MediaPortal project is to create a free open source media centre application, which supports all advanced media centre functions, and is accessible to all Windows users.

    In reaching this goal we are working every day to make sure our software is one of the best.

             

  • Support MediaPortal!

    The team works very hard to make sure the community is running the best HTPC-software. We give away MediaPortal for free but hosting and software is not for us.

    Care to support our work with a few bucks? We'd really appreciate it!