Logo Search packages:      
Sourcecode: webissues version File versions  Download package

connectionmanager.cpp

/**************************************************************************
* This file is part of the WebIssues program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007 WebIssues Team
*
* 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 2 of the License, or
* (at your option) any later version.
**************************************************************************/

#include "connectionmanager.h"

#include <QSettings>
#include <QDir>

#if defined( Q_WS_WIN )
#define _WIN32_IE 0x0400
#include <shlobj.h>
#endif

#include "commands/commandmanager.h"
#include "data/datamanager.h"
#include "data/connectioninfo.h"
#include "data/attachmentscache.h"
#include "viewmanager.h"

using namespace WebIssues;

00030 ConnectionManager* WebIssues::connectionManager = NULL;

00032 ConnectionManager::ConnectionManager() :
    m_info( NULL ),
    m_settings( NULL ),
    m_attachmentsCache( NULL )
{
    QString homePath = QDir::homePath();

#if defined( Q_WS_WIN )
    char appDataPath[ MAX_PATH ];
    if ( SHGetSpecialFolderPathA( 0, appDataPath, CSIDL_APPDATA, FALSE ) ) {
        m_dataPath = QDir::fromNativeSeparators( QString::fromLocal8Bit( appDataPath ) );
        m_dataPath += "/WebIssues";
    } else {
        m_dataPath = homePath + "/WebIssues";
    }

    char localAppDataPath[ MAX_PATH ];
    if ( SHGetSpecialFolderPathA( 0, localAppDataPath, CSIDL_LOCAL_APPDATA, FALSE ) ) {
        m_cachePath = QDir::fromNativeSeparators( QString::fromLocal8Bit( localAppDataPath ) );
        m_cachePath += "/WebIssues";
    } else {
        m_cachePath = m_dataPath + "/.cache";
    }
#else
    char* dataHome = getenv( "XDG_DATA_HOME" );
    if ( dataHome ) {
        if ( dataHome[ 0 ] == '/' )
            m_dataPath = dataHome;
        else
            m_dataPath = homePath + '/' + dataHome;
    } else {
        m_dataPath = homePath + "/.local/share";
    }

    m_dataPath += "/webissues";

    char* cacheHome = getenv( "XDG_CACHE_HOME" );
    if ( cacheHome ) {
        if ( cacheHome[ 0 ] == '/' )
            m_cachePath = cacheHome;
        else
            m_cachePath = homePath + '/' + cacheHome;
    } else {
        m_cachePath = homePath + "/.cache";
    }

    m_cachePath += "/webissues";
#endif
}

00082 ConnectionManager::~ConnectionManager()
{
    closeConnection();
}

00087 void ConnectionManager::createCommandManager()
{
    commandManager = new CommandManager();
}

00092 void ConnectionManager::openConnection( ConnectionInfo* info )
{
    dataManager = new DataManager();
    viewManager = new ViewManager();

    m_info = info;

    QString path = locateDataFile( "server.conf" );

    m_settings = new QSettings( path, QSettings::IniFormat );

    m_attachmentsCache = new AttachmentsCache();
}

00106 void ConnectionManager::closeConnection()
{
    delete viewManager;
    viewManager = NULL;

    delete dataManager;
    dataManager = NULL;

    commandManager->deleteLater();
    commandManager = NULL;

    delete m_info;
    m_info = NULL;

    delete m_settings;
    m_settings = NULL;

    delete m_attachmentsCache;
    m_attachmentsCache = NULL;
}

00127 QString ConnectionManager::locateDataFile( const QString& name )
{
    QString path = m_dataPath + '/' + m_info->serverUuid() + '/' + name;

    checkAccess( path );

    return path;
}

00136 QString ConnectionManager::locateCacheFile( const QString& name )
{
    QString path = m_cachePath + '/' + m_info->serverUuid() + '/' + name;

    checkAccess( path );

    return path;
}

bool ConnectionManager::checkAccess( const QString& path )
{
    QFileInfo fileInfo( path );

    if ( fileInfo.exists() )
        return fileInfo.isReadable();

    QDir dir = QDir::root();

    QStringList pathParts = path.split( '/', QString::SkipEmptyParts );

    for ( int i = 0; i < pathParts.size() - 1; i++ ) {
        const QString& part = pathParts.at( i );

        if ( dir.cd( part ) )
            continue;

        if ( dir.mkdir( part ) && dir.cd( part ) )
            continue;

        return false;
    }

    return true;
}

#include "connectionmanager.moc"

Generated by  Doxygen 1.6.0   Back to index