Development/Tutorials/Plasma4/JavaScript/API-LocalIO

From KDE TechBase
Revision as of 20:02, 22 February 2015 by Cgimenez (talk | contribs) (Adding an example for LocalIO)

LocalIO

This Extension provides access to local files using asynchronous IOJob objects.

Functions:

  • IOJob getUrl(Url url): attempts to fetch the file using an IOJob
  • IOJob getUrl(String url): attempts to fetch the file using an IOJob
  • String userDataPath([String type, String path]): (scripting version >= 4) returns the default path for user data. Called with no parameters, it returns the user's home directory. If only one string is passed in, the standard directory for that type of data in the user's home directory will be located; the following values are recognized:
    • documents
    • music
    • video
    • downloads
    • pictures
    • autostart
    • desktop (should be considered deprecated for Plasma workspaces)

If a second string is passed in, it is considered a request for a specific path and the following types are recognized:

    • apps - Applications menu (.desktop files).
    • autostart - Autostart directories (both XDG and kde-specific)
    • cache - Cached information (e.g. favicons, web-pages)
    • cgi - CGIs to run from kdehelp.
    • config - Configuration files.
    • data - Where applications store data.
    • emoticons - Emoticons themes
    • exe - Executables in $prefix/bin. findExe() for a function that takes $PATH into account.
    • html - HTML documentation.
    • icon - Icons, see KIconLoader.
    • kcfg - KConfigXT config files.
    • lib - Libraries.
    • locale - Translation files for KLocale.
    • mime - Mime types defined by KDE-specific .desktop files.
    • module - Module (dynamically loaded library).
    • qtplugins - Qt plugins (dynamically loaded objects for Qt)
    • services - Services.
    • servicetypes - Service types.
    • sound - Application sounds.
    • templates - Templates for the "Create new file" functionality.
    • wallpaper - Wallpapers.
    • tmp - Temporary files (specific for both current host and current user)
    • socket - UNIX Sockets (specific for both current host and current user)
    • xdgconf-menu - Freedesktop.org standard location for menu layout (.menu) files.
    • xdgdata-apps - Freedesktop.org standard location for application desktop files.
    • xdgdata-dirs - Freedesktop.org standard location for menu descriptions (.directory files).
    • xdgdata-mime - Freedesktop.org standard location for MIME type definitions.
    • xdgdata-icon - Freedesktop.org standard location for icons.
    • xdgdata-pixmap - Gnome-compatibility location for pixmaps.

The second parameter should be a specific resource to find the path to. An example might be userDataPath("data", "plasma-desktop").

Example

This example will load a file given its path. filedata is a plasmoid Label or TextArea where to write the information loaded.

function loadFile(filepath){
    var iojb;
    filedata.text = ""; // First, delete the textarea information.
    iojb = plasmoid.getUrl(filepath);
    iojb.data.connect(loadData);
}

// Remember data is a ByteArray, so we need to translate this bytes numbers into ASCII(or UTF8) characters.
function loadData(iojb, data){
    var str = "";
    var i;

    if (data.length > 0){
        for (i = 0; i < data.length; i++){
            str = str + String.fromCharCode(data[i]);
        }
        //print(str);
        filedata.text = filedata.text + str;
    }
}

Remember that signals are connected using the connect(...) method as explained at Plasma using Javascript: Getting Started