Giter Site home page Giter Site logo

standardpaths's Introduction

Standard paths

D library for getting standard paths (e.g. Pictures, Music, Documents and also generic configuration and data paths). Inspired by QStandardPaths from Qt.

Build Status Windows Build Status

Online documentation

Platform support

Works on Freedesktop (GNU/Linux, FreeBSD, etc.), Windows and OS X.

Running examples

Prints some standard paths to stdout.

dub examples/printdirs.d

On OSX it also can be built to use Cocoa instead of Carbon:

dub --single examples/printdirs.d --override-config=standardpaths/cocoa

Get path of given type, verify it exists or create if it does not.

dub examples/getpath.d --verify --create templates

Use Cocoa instead of Carbon on OSX:

dub --single examples/getpath.d --override-config=standardpaths/cocoa -- --create music

With subfolder:

dub examples/getpath.d --subfolder=MyLittleCompany/MyLittleApplication data

Use cases

Some code snippets showing how standardpaths library is supposed to be used.

Building file dialogs

Let's say you have some fancy FileDialog class and you want to provide shortcuts to standard user directories to improve experience. Your code may look like this:

import standardpaths;
import std.file;
import std.stdio;

void showFileDialog()
{
    auto fileDialog = new FileDialog;
    auto folderFlag = FolderFlag.verify;

    string[] paths = [
        homeDir(),
        writablePath(StandardPath.desktop, folderFlag),
        writablePath(StandardPath.downloads, folderFlag),
        writablePath(StandardPath.documents, folderFlag),
        writablePath(StandardPath.pictures, folderFlag),
        writablePath(StandardPath.music, folderFlag),
        writablePath(StandardPath.videos, folderFlag),
        writablePath(StandardPath.templates, folderFlag),
        writablePath(StandardPath.publicShare, folderFlag)
    ];
    foreach(path; paths) {
        if (path.length) {
            string label = path.baseName();
            fileDialog.addPath(label, path);
        }
    }
    fileDialog.show();
}

Writing configuration files

Usually your application will have some configuration file (or files) to store user's preferences and settings. That's where you could use StandardPath.config path. While the library returns generic paths for configuration, data and cache, you want to have separate folders specially for your application, so you will not accidentally read or modify files used by other programs. Usually these paths are built by concatenating of generic path, organization name and application name.

//You may have these as constants somewhere in your code
enum organizationName = "MyLittleCompany";
enum applicationName = "MyLittleApplication";

import standardpaths;
import std.stdio;
import std.path;

void saveSettings(const Config config)
{
    string configDir = writablePath(StandardPath.config, buildPath(organizationName, applicationName), FolderFlag.create);
    if (!configDir.length) {
        throw new Exception("Could not create config directory");
    }
    string configFile = buildPath(configDir, "config.conf");

    auto f = File(configFile, "w"); 
    // write settings
    writeln("Settings saved!");
}

Reading configuration files

Since one can save settings it also should be able to read them. Before the first start application does not have any user-specific settings, though it may provide some global default settings upon installing. It's up to developer to decide how to read configs, e.g. whether to read the first found file only or to merge settings from all found config consequentially.

Config readSettings()
{
    string[] configDirs = standardPaths(StandardPath.config, buildPath(organizationName, applicationName));

    foreach(configDir; configDirs) {
        string configFile = buildPath(configDir, "config.conf");
        if (configFile.exists) {
            auto f = File(configFile, "r");
            Config config;
            //read settings...
            return config;//consider using of the first found file
        }
    }
}

Implementation details

Freedesktop

On freedesktop systems (GNU/Linux, FreeBSD, etc.) library follows XDG Base Directory Specification and also provides behavior similiar to xdg-user-dirs.

Windows

On Windows it utilizes SHGetKnownFolderPath or SHGetSpecialFolderPath as fallback.

Mac OS X

Depending on configuration the library uses FSFindFolder from Carbon framework or URLForDirectory from Cocoa. See here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.