示例#1
0
void ResourceView::addResource()
{
  bool ok = false;
  KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
  ResourceItem *i = static_cast<ResourceItem*>( mListView->selectedItem() );
  if ( i && ( i->isSubresource() || i->resource()->canHaveSubresources() ) ) {
    const QString folderName = KInputDialog::getText( i18n( "Add Subresource" ),
            i18n( "Please enter a name for the new subresource" ), QString::null,
            &ok, this );
    if ( !ok )
      return;
    const QString parentId = i->isSubresource() ? i->resourceIdentifier() : QString:: null;
    if ( !i->resource()->addSubresource( folderName, parentId ) ) {
      KMessageBox::error( this, i18n("<qt>Unable to create subresource <b>%1</b>.</qt>")
                                .arg( folderName ) );
    }
    return;
  }

  QStringList types = manager->resourceTypeNames();
  QStringList descs = manager->resourceTypeDescriptions();
  QString desc = KInputDialog::getItem( i18n( "Resource Configuration" ),
      i18n( "Please select type of the new resource:" ), descs, 0, false, &ok,
            this );
  if ( !ok )
    return;

  QString type = types[ descs.findIndex( desc ) ];

  // Create new resource
  ResourceCalendar *resource = manager->createResource( type );
  if( !resource ) {
    KMessageBox::error( this, i18n("<qt>Unable to create resource of type <b>%1</b>.</qt>")
                              .arg( type ) );
    return;
  }

  resource->setResourceName( i18n("%1 resource").arg( type ) );

  KRES::ConfigDialog *dlg = new KRES::ConfigDialog( this, QString("calendar"), resource,
                          "KRES::ConfigDialog" );

  if ( dlg && dlg->exec() ) {
    resource->setTimeZoneId( KOPrefs::instance()->mTimeZoneId );
    if ( resource->isActive() ) {
      resource->open();
      resource->load();
    }
    manager->add( resource );
    // we have to call resourceAdded manually, because for in-process changes
    // the dcop signals are not connected, so the resource's signals would not
    // be connected otherwise
    mCalendar->resourceAdded( resource );
  } else {
    delete resource;
    resource = 0;
  }
  if ( dlg ) delete dlg;
  emitResourcesChanged();
}
void ResourceView::addResource()
{
  bool ok = false;
  KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
  ResourceItem *i = mSelectedParent;

  if ( i && ( i->isSubresource() || i->resource()->canHaveSubresources() ) ) {
    const QString folderName =
      KInputDialog::getText( i18n( "Add Calendar Folder" ),
                             i18n( "Please enter a name for the new calendar folder" ),
                             QString(), &ok, this );
    if ( !ok ) {
      return;
    }

    const QString parentId = i->isSubresource() ? i->resourceIdentifier() : QString();
    if ( !i->resource()->addSubresource( folderName, parentId ) ) {
      KMessageBox::error(
        this,
        i18n( "<qt>Unable to create the calendar folder <b>%1</b>.</qt>",
              folderName ) );
    }
    return;
  }

  QStringList types = manager->resourceTypeNames();
  QStringList descs = manager->resourceTypeDescriptions();
  QString desc =
    KInputDialog::getItem( i18n( "Calendar Configuration" ),
                           i18n( "Please select the type of the new calendar:" ),
                           descs, 0, false, &ok, this );
  if ( !ok ) {
    return;
  }

  QString type = types.at( descs.indexOf( desc ) );

  // Create new resource
  ResourceCalendar *resource = manager->createResource( type );
  if( !resource ) {
    KMessageBox::error( this,
                        i18n( "<qt>Unable to create a calendar of type <b>%1</b>.</qt>", type ) );
    return;
  }

  resource->setResourceName( i18n( "%1 calendar", type ) );

  // TODO: Add a fallback (KColorCollection::setName() broken?)
  KColorCollection collection( "Oxygen.colors" );
  // TODO: Be smarter than this
  int rand = ( KRandom::random() % collection.count() ) + 1;
  QColor color = collection.color( rand );

  KOPrefs::instance()->setResourceColor( resource->identifier(), color );

  bool success = true;
  QPointer<KRES::ConfigDialog> dlg =
    new KRES::ConfigDialog( this, QString( "calendar" ), resource );

  if ( dlg->exec() != QDialog::Accepted ) {
    success = false;
  }
  delete dlg;

  if ( success ) {
    resource->setTimeSpec( KOPrefs::instance()->timeSpec() );
    if ( resource->isActive() && ( !resource->open() || !resource->load() ) ) {
      // ### There is a resourceLoadError() signal declared in ResourceCalendar
      //     but no subclass seems to make use of it. We could do better.
      KMessageBox::error( this, i18n( "Unable to create the calendar." ) );
      success = false;
    }
  }

  if ( success ) {
    manager->add( resource );
    // we have to call resourceAdded manually, because for in-process changes
    // the dcop signals are not connected, so the resource's signals would not
    // be connected otherwise
    mCalendar->resourceAdded( resource );
  }

  if ( !success ) {
    delete resource;
    resource = 0;
  }

  //### maybe only do this if ( success )
  emitResourcesChanged();
}