<commit_before>/*
    Copyright (c) 2006 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "resourcemanager.h"
#include "tracer.h"
#include "storage/datastore.h"

#include <QDBusConnection>

using namespace Akonadi;

ResourceManager* ResourceManager::mSelf = 0;

Akonadi::ResourceManager::ResourceManager(QObject * parent) :
  QObject( parent )
{
  mManager = new org::kde::Akonadi::AgentManager( "org.kde.Akonadi.Control", "/AgentManager", QDBusConnection::sessionBus(), this );

  connect( mManager, SIGNAL(agentInstanceAdded(const QString&)),
           SLOT(resourceAdded(const QString&)) );
  connect( mManager, SIGNAL(agentInstanceRemoved(const QString&)),
           SLOT(resourceRemoved(const QString& )) );
}

void Akonadi::ResourceManager::resourceAdded(const QString & name)
{
  DataStore *db = new DataStore();
  db->init();

  Resource resource = db->resourceByName( name.toUtf8() );
  if ( resource.isValid() )
    return; // resource already exists

  // create the resource
  if ( !db->appendResource( name ) ) {
    Tracer::self()->error( "ResourceManager", "Could not create resource '" + name + "'." );
    return;
  }
  resource = db->resourceByName( name.toUtf8() );

  // create the toplevel collection
  QString collectionName = mManager->agentName( mManager->agentInstanceType( name ) );
  if ( collectionName.isEmpty() )
    collectionName = name;
  db->appendLocation( collectionName, resource );

  delete db;
}

void Akonadi::ResourceManager::resourceRemoved(const QString & name)
{
  DataStore *db = new DataStore();
  db->init();
<commit_msg>Try harder to create the resource top-level collection.<commit_after>/*
    Copyright (c) 2006 <NAME> <<EMAIL>>

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "resourcemanager.h"
#include "tracer.h"
#include "storage/datastore.h"

#include <QDBusConnection>

using namespace Akonadi;

ResourceManager* ResourceManager::mSelf = 0;

Akonadi::ResourceManager::ResourceManager(QObject * parent) :
  QObject( parent )
{
  mManager = new org::kde::Akonadi::AgentManager( "org.kde.Akonadi.Control", "/AgentManager", QDBusConnection::sessionBus(), this );

  connect( mManager, SIGNAL(agentInstanceAdded(const QString&)),
           SLOT(resourceAdded(const QString&)) );
  connect( mManager, SIGNAL(agentInstanceRemoved(const QString&)),
           SLOT(resourceRemoved(const QString& )) );
}

void Akonadi::ResourceManager::resourceAdded(const QString & name)
{
  DataStore *db = new DataStore();
  db->init();

  Resource resource = db->resourceByName( name.toUtf8() );
  if ( resource.isValid() )
    return; // resource already exists

  // create the resource
  if ( !db->appendResource( name ) ) {
    Tracer::self()->error( "ResourceManager", "Could not create resource '" + name + "'." );
    return;
  }
  resource = db->resourceByName( name.toUtf8() );

  // create the toplevel collection
  QString collectionName = mManager->agentName( mManager->agentInstanceType( name ) );
  if ( collectionName.isEmpty() )
    collectionName = name;
  Location loc = db->locationByName( collectionName.toUtf8() );
  if ( loc.isValid() )
    collectionName = name; // name already in use...
  db->appendLocation( collectionName, resource );

  delete db;
}

void Akonadi::ResourceManager::resourceRemoved(const QString & name)
{
  DataStore *db = new DataStore();
  db->init();

{'fraction_non_alphanumeric': 0.10787953795379537, 'fraction_numerical': 0.008044554455445545, 'mean_word_length': 4.082809224318658, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '915854', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1354, 'n_words': 471}