PageRenderTime 93ms CodeModel.GetById 40ms app.highlight 16ms RepoModel.GetById 34ms app.codeStats 0ms

/src/libtomahawk/aclsystem.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 152 lines | 110 code | 21 blank | 21 comment | 15 complexity | b3b4bf68f340c37ba8b769adacf17eb3 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "aclsystem.h"
 20
 21#include <QMutexLocker>
 22#include <QVariant>
 23
 24#include "tomahawksettings.h"
 25
 26#include "utils/logger.h"
 27
 28
 29ACLSystem* ACLSystem::s_instance = 0;
 30
 31ACLSystem*
 32ACLSystem::instance()
 33{
 34    if( !s_instance )
 35        new ACLSystem();
 36    return s_instance;
 37}
 38
 39
 40ACLSystem::ACLSystem( QObject* parent )
 41    : QObject( parent ),
 42      m_saveTimer( this )
 43{
 44    s_instance = this;
 45    //qRegisterMetaType< QHash< QString, QHash< QString, ACL > > >("ACLSystem::ACLCacheHash");
 46
 47    QStringList savedEntries = TomahawkSettings::instance()->aclEntries();
 48    if( !savedEntries.empty() && savedEntries.size() % 3 == 0 )
 49    {
 50        int index = 0;
 51        while( index < savedEntries.length() )
 52        {
 53            if( !m_cache.contains( savedEntries.at( index ) ) )
 54                m_cache[savedEntries.at( index ) ] = QHash< QString, ACL >();
 55            m_cache[savedEntries.at( index )][savedEntries.at( index + 1 )] = (ACL)(savedEntries.at( index + 2 ).toInt() );
 56            index += 3;
 57        }
 58    }
 59
 60    m_saveTimer.setSingleShot( false );
 61    m_saveTimer.setInterval( 60000 );
 62    connect( &m_saveTimer, SIGNAL( timeout() ), this, SLOT( saveTimerFired() ) );
 63    m_saveTimer.start();
 64}
 65
 66ACLSystem::~ACLSystem()
 67{
 68    m_saveTimer.stop();
 69    saveTimerFired();
 70}
 71
 72ACLSystem::ACL
 73ACLSystem::isAuthorizedUser( const QString& dbid )
 74{
 75//    qDebug() << Q_FUNC_INFO;
 76    QMutexLocker locker( &m_cacheMutex );
 77    qDebug() << "Current cache keys =" << m_cache.keys();
 78//    qDebug() << "Looking up dbid";
 79    if( !m_cache.contains( dbid ) )
 80        return ACLSystem::NotFound;
 81    else
 82    {
 83        QHash< QString, ACL > peerHash = m_cache[dbid];
 84        if( peerHash.contains( "global" ) )
 85            return peerHash["global"];
 86        return ACLSystem::NotFound;
 87    }
 88}
 89
 90void
 91ACLSystem::authorizeUser( const QString& dbid, ACLSystem::ACL globalType )
 92{
 93//    qDebug() << Q_FUNC_INFO;
 94    if( globalType == ACLSystem::NotFound )
 95        return;
 96
 97    QMutexLocker locker( &m_cacheMutex );
 98
 99    QHash< QString, ACL > peerHash;
100    if( m_cache.contains( dbid ) )
101        peerHash = m_cache[dbid];
102    peerHash["global"] = globalType;
103    m_cache[dbid] = peerHash;
104}
105
106ACLSystem::ACL
107ACLSystem::isAuthorizedPath( const QString& dbid, const QString& path )
108{
109    QMutexLocker locker( &m_cacheMutex );
110
111    if( !m_cache.contains( dbid ) )
112        return ACLSystem::NotFound;
113
114    QHash< QString, ACL > peerHash = m_cache[dbid];
115    if( !peerHash.contains( path ) )
116    {
117        if( peerHash.contains( "global" ) )
118            return peerHash["global"];
119        else
120            return ACLSystem::Deny;
121    }
122    return peerHash[path];
123}
124
125void
126ACLSystem::authorizePath( const QString& dbid, const QString& path, ACLSystem::ACL type )
127{
128    TomahawkSettings *s = TomahawkSettings::instance();
129    if( !s->scannerPaths().contains( path ) )
130    {
131        qDebug() << "path selected is not in our scanner path!";
132        return;
133    }
134    QMutexLocker locker( &m_cacheMutex );
135    QHash< QString, ACLSystem::ACL > peerHash;
136    if ( m_cache.contains( dbid ) )
137        peerHash = m_cache[dbid];
138    peerHash[path] = type;
139    m_cache[dbid] = peerHash;
140}
141
142void
143ACLSystem::saveTimerFired()
144{
145    QStringList saveCache;
146    foreach( QString dbid, m_cache.keys() )
147    {
148        foreach( QString path, m_cache[dbid].keys() )
149            saveCache << dbid << path << QString::number( (int)(m_cache[dbid][path]) );
150    }
151    TomahawkSettings::instance()->setAclEntries( saveCache );
152}