pathutils.cpp 4.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/**
 ******************************************************************************
 *
 * @file       pathutils.cpp
 * @author     The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
 * @brief      Utilities to find the location of openpilot GCS files:
 *             - Plugins Share directory path
 *
 * @see        The GNU Public License (GPL) Version 3
 *
 *****************************************************************************/
/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include "pathutils.h"
#include "xmlconfig.h"
#include <stdint.h>
#include <QDebug>


namespace Utils {

    PathUtils::PathUtils()
    {

    }

    /**
      Returns the base path of the share directory.

      Path is in Qt/Unix conventions, separated by "/".
      */
QString PathUtils::GetDataPath()
{
    // This routine works with "/" as the standard:
    // Figure out root:  Up one from 'bin'
    QDir rootDir = QApplication::applicationDirPath();
    rootDir.cdUp();
    const QString rootDirPath = rootDir.canonicalPath();
    QString dataPath = rootDirPath;
    dataPath += QLatin1Char('/');
    // FIXME XXX
#ifdef EXTERNAL_USE
    dataPath += QLatin1String("data");
#else
    dataPath += QLatin1String(GCS_DATA_BASENAME);
#endif
    dataPath += QLatin1Char('/');
   return dataPath;
}

/**
  Cuts the standard data path from the 'path' argument. If path does not start
with the standard data path, then do nothing.

   Always returns a path converted to "/".
  */
QString PathUtils::RemoveDataPath(QString path)
{
    // Depending on the platform, we might get either "/" or "\"
    // so we need to go to the standard ("/")
    QString goodPath = QDir::fromNativeSeparators(path);
    if (goodPath.startsWith(GetDataPath())) {
        int i = goodPath.length()- GetDataPath().length();
        return QString("%%DATAPATH%%") + goodPath.right(i);
    } else
        return goodPath;
}

/**
  Inserts the data path (only if the path starts with %%DATAPATH%%)

  Returns a "/" or "\" separated path depending on platform conventions.
  */
QString PathUtils::InsertDataPath(QString path)
{
    if (path.startsWith(QString("%%DATAPATH%%")))
    {
        QString newPath = GetDataPath();
        newPath += path.right(path.length()-12);
        return QDir::toNativeSeparators(newPath);
    }
    return QDir::toNativeSeparators(path);
}

/**
  Gets a standard user-writable location for the system
  */
QString PathUtils::GetStoragePath()
{
    // This routine works with "/" as the standard:
    // Work out where the settings are stored on the machine
    QSettings set(XmlConfig::XmlSettingsFormat, QSettings::UserScope,QLatin1String("OpenPilot"), QLatin1String("OpenPilotGCS"));
    QFileInfo f(set.fileName());
    QDir dir(f.absoluteDir());

    const QString homeDirPath = dir.canonicalPath();
    QString storagePath = homeDirPath;
    storagePath += QLatin1Char('/');
    // storagePath += QLatin1String("OpenPilot");
    // storagePath += QLatin1Char('/');
   return storagePath;
}

/**
  Removes the standard storage path and replace with a tag
  */
QString PathUtils::RemoveStoragePath(QString path)
{
    // Depending on the platform, we might get either "/" or "\"
    // so we need to go to the standard ("/")
    QString goodPath = QDir::fromNativeSeparators(path);
    if (goodPath.startsWith(GetStoragePath())) {
        int i = goodPath.length()- GetStoragePath().length();
        return QString("%%STOREPATH%%") + goodPath.right(i);
    } else
        return goodPath;
}

/**
  Inserts the standard storage path is there is a storage path tag
  */
QString PathUtils::InsertStoragePath(QString path)
{
    if (path.startsWith(QString("%%STOREPATH%%")))
    {
        QString newPath = GetStoragePath();
        newPath += path.right(path.length()-13);
        return QDir::toNativeSeparators(newPath);
    }
    return QDir::toNativeSeparators(path);

}

}