void ResourceView::addResourceItem(ResourceCalendar *resource) { ResourceItem *item = new ResourceItem(resource, this, mListView); // assign a color, but only if this is a resource that actually // hold items at top level if(!resource->canHaveSubresources() || resource->subresources().isEmpty()) { QColor resourceColor = *KOPrefs::instance()->resourceColor(resource->identifier()); item->setResourceColor(resourceColor); item->update(); } connect(resource, SIGNAL(signalSubresourceAdded(ResourceCalendar *, const QString &, const QString &, const QString &)), SLOT(slotSubresourceAdded(ResourceCalendar *, const QString &, const QString &, const QString &))); connect(resource, SIGNAL(signalSubresourceRemoved(ResourceCalendar *, const QString &, const QString &)), SLOT(slotSubresourceRemoved(ResourceCalendar *, const QString &, const QString &))); connect(resource, SIGNAL(resourceSaved(ResourceCalendar *)), SLOT(closeResource(ResourceCalendar *))); updateResourceList(); emit resourcesChanged(); }
void ResourceView::updateResourceItem( ResourceCalendar *resource ) { ResourceItem *item = findItem( resource ); if ( item ) { item->update(); } }
// Reads data from an existing resource // (deprecated, use LoadResource() instead). status_t BResources::ReadResource(type_code type, int32 id, void* data, off_t offset, size_t length) { status_t error = (data && offset >= 0 ? B_OK : B_BAD_VALUE); if (error == B_OK) error = InitCheck(); ResourceItem* item = NULL; if (error == B_OK) { item = fContainer->ResourceAt(fContainer->IndexOf(type, id)); if (!item) error = B_BAD_VALUE; } if (error == B_OK && fResourceFile) error = fResourceFile->ReadResource(*item); if (error == B_OK) { if (item) { ssize_t read = item->ReadAt(offset, data, length); if (read < 0) error = read; } else error = B_BAD_VALUE; } return error; }
// Adds a new resource to the file. status_t BResources::AddResource(type_code type, int32 id, const void* data, size_t length, const char* name) { status_t error = (data ? B_OK : B_BAD_VALUE); if (error == B_OK) error = InitCheck(); if (error == B_OK) error = (fReadOnly ? B_NOT_ALLOWED : B_OK); if (error == B_OK) { ResourceItem* item = new(nothrow) ResourceItem; if (!item) error = B_NO_MEMORY; if (error == B_OK) { item->SetIdentity(type, id, name); ssize_t written = item->WriteAt(0, data, length); if (written < 0) error = written; else if (written != (ssize_t)length) error = B_ERROR; } if (error == B_OK) { if (!fContainer->AddResource(item)) error = B_NO_MEMORY; } if (error != B_OK) delete item; } return error; }
// Writes data into an existing resource // (deprecated, use AddResource() instead). status_t BResources::WriteResource(type_code type, int32 id, const void* data, off_t offset, size_t length) { status_t error = (data && offset >= 0 ? B_OK : B_BAD_VALUE); if (error == B_OK) error = InitCheck(); if (error == B_OK) error = (fReadOnly ? B_NOT_ALLOWED : B_OK); ResourceItem *item = NULL; if (error == B_OK) { item = fContainer->ResourceAt(fContainer->IndexOf(type, id)); if (!item) error = B_BAD_VALUE; } if (error == B_OK && fResourceFile) error = fResourceFile->ReadResource(*item); if (error == B_OK) { if (item) { ssize_t written = item->WriteAt(offset, data, length); if (written < 0) error = written; else if (written != (ssize_t)length) error = B_ERROR; } } return error; }
void ResourceView::showInfo() { ResourceItem *item = currentItem(); if ( !item ) return; QString txt = "<qt>" + item->resource()->infoText() + "</qt>"; KMessageBox::information( this, txt ); }
void ResourceView::saveResource() { ResourceItem *item = currentItem(); if ( !item ) return; ResourceCalendar *r = item->resource(); r->save(); }
ResourceItem *ResourceView::findItem( ResourceCalendar *r ) { QList<QTreeWidgetItem *> items = mListView->findItems( "*", Qt::MatchWildcard ); foreach ( QTreeWidgetItem *i, items ) { ResourceItem *item = static_cast<ResourceItem *>( i ); if ( item->resource() == r ) { return item; } }
void ResourceView::updateResourceList() { QListViewItemIterator it( mListView ); ResourceCalendar* stdRes = mCalendar->resourceManager()->standardResource(); while ( it.current() ) { ResourceItem *item = static_cast<ResourceItem *>( it.current() ); item->setStandardResource( item->resource() == stdRes ); ++it; } }
ResourceItem *ResourceView::findItem( ResourceCalendar *r ) { QListViewItem *item; ResourceItem *i = 0; for( item = mListView->firstChild(); item; item = item->nextSibling() ) { i = static_cast<ResourceItem *>( item ); if ( i->resource() == r ) break; } return i; }
void ResourceView::setStandard() { ResourceItem *item = currentItem(); if ( !item ) return; ResourceCalendar *r = item->resource(); KCal::CalendarResourceManager *manager = mCalendar->resourceManager(); manager->setStandardResource( r ); updateResourceList(); }
void ResourceView::currentChanged( QListViewItem *item ) { ResourceItem *i = currentItem(); if ( !item || i->isSubresource() ) { mDeleteButton->setEnabled( false ); mEditButton->setEnabled( false ); } else { mDeleteButton->setEnabled( true ); mEditButton->setEnabled( true ); } }
ResourceItem *ResourceView::findItemByIdentifier( const QString& id ) { QListViewItem *item; ResourceItem *i = 0; for( item = mListView->firstChild(); item; item = item->itemBelow() ) { i = static_cast<ResourceItem *>( item ); if ( i->resourceIdentifier() == id ) return i; } return 0; }
int32 ResourcesContainer::IndexOf(type_code type, int32 id) const { int32 index = -1; int32 count = CountResources(); for (int32 i = 0; index == -1 && i < count; i++) { ResourceItem *item = ResourceAt(i); if (item->Type() == type && item->ID() == id) index = i; } return index; }
// Gets information about a resource identified by byType and andName. bool BResources::GetResourceInfo(type_code byType, const char* andName, int32* idFound, size_t* lengthFound) { ResourceItem* item = NULL; if (InitCheck() == B_OK) item = fContainer->ResourceAt(fContainer->IndexOf(byType, andName)); if (item) { if (idFound) *idFound = item->ID(); if (lengthFound) *lengthFound = item->DataSize(); } return item; }
int32 ResourcesContainer::IndexOfType(type_code type, int32 typeIndex) const { int32 index = -1; int32 count = CountResources(); for (int32 i = 0; index == -1 && i < count; i++) { ResourceItem *item = ResourceAt(i); if (item->Type() == type) { if (typeIndex == 0) index = i; typeIndex--; } } return index; }
void ResourceView::editResource() { ResourceItem *item = currentItem(); if (!item) return; ResourceCalendar *resource = item->resource(); KRES::ConfigDialog dlg( this, QString("calendar"), resource, "KRES::ConfigDialog" ); if ( dlg.exec() ) { item->setText( 0, resource->resourceName() ); mCalendar->resourceManager()->change( resource ); } emitResourcesChanged(); }
// IndexOf int32 ResourcesContainer::IndexOf(type_code type, const char *name) const { int32 index = -1; int32 count = CountResources(); for (int32 i = 0; index == -1 && i < count; i++) { ResourceItem *item = ResourceAt(i); const char *itemName = item->Name(); if (item->Type() == type && (name == NULL && itemName == NULL || name != NULL && itemName != NULL && !strcmp(name, itemName))) { index = i; } } return index; }
// Add a new entry void ResourceView::slotSubresourceAdded( ResourceCalendar *calendar, const QString& /*type*/, const QString& resource, const QString& label) { QListViewItem *i = mListView->findItem( calendar->resourceName(), 0 ); if ( !i ) // Not found return; if ( findItemByIdentifier( resource ) ) return; ResourceItem *item = static_cast<ResourceItem *>( i ); ResourceItem *newItem = new ResourceItem( calendar, resource, label, this, item ); QColor resourceColor = *KOPrefs::instance()->resourceColor( resource ); newItem->setResourceColor( resourceColor ); }
// PrintToStream void ResourceFile::PrintToStream(bool longInfo) { if (longInfo) { off_t resourcesOffset = fFile.GetOffset(); printf("ResourceFile:\n"); printf("file type : %s\n", kFileTypeNames[fFileType]); printf("endianess : %s\n", (fHostEndianess == (bool)B_HOST_IS_LENDIAN) ? "little" : "big"); printf("resource section offset: 0x%08Lx (%Ld)\n", resourcesOffset, resourcesOffset); if (fInfoTableItem) { int32 offset = fInfoTableItem->GetOffset(); int32 size = fInfoTableItem->GetSize(); printf("resource info table : offset: 0x%08lx (%ld), " "size: 0x%08lx (%ld)\n", offset, offset, size, size); } printf("number of resources : %ld\n", fResourceCount); for (int32 i = 0; i < fResourceCount; i++) { ResourceItem* item = ItemAt(i); item->PrintToStream(); } } else { printf(" Type ID Size Name\n"); printf(" ------ ----- -------- --------------------\n"); for (int32 i = 0; i < fResourceCount; i++) { ResourceItem* item = ItemAt(i); type_code type = item->GetType(); char typeName[4] = { type >> 24, (type >> 16) & 0xff, (type >> 8) & 0xff, type & 0xff }; printf(" '%.4s' %5ld %8lu %s\n", typeName, item->GetID(), item->GetSize(), item->GetName()); } } }
void ResourceItem::createSubresourceItems() { const QStringList subresources = mResource->subresources(); if ( !subresources.isEmpty() ) { setOpen( true ); setExpandable( true ); // This resource has subresources QStringList::ConstIterator it; for ( it=subresources.begin(); it!=subresources.end(); ++it ) { ResourceItem *item = new ResourceItem( mResource, *it, mResource->labelForSubresource( *it ), mView, this ); QColor resourceColor = *KOPrefs::instance()->resourceColor( *it ); item->setResourceColor( resourceColor ); item->update(); } } mSubItemsCreated = true; }
void ResourceView::contextMenuRequested(QListViewItem *i, const QPoint &pos, int) { KCal::CalendarResourceManager *manager = mCalendar->resourceManager(); ResourceItem *item = static_cast<ResourceItem *>(i); QPopupMenu *menu = new QPopupMenu(this); connect(menu, SIGNAL(aboutToHide()), menu, SLOT(deleteLater())); if(item) { int reloadId = menu->insertItem(i18n("Re&load"), this, SLOT(reloadResource())); menu->setItemEnabled(reloadId, item->resource()->isActive()); int saveId = menu->insertItem(i18n("&Save"), this, SLOT(saveResource())); menu->setItemEnabled(saveId, item->resource()->isActive()); menu->insertSeparator(); menu->insertItem(i18n("Show &Info"), this, SLOT(showInfo())); //FIXME: This is better on the resource dialog if(KOPrefs::instance()->agendaViewColors() != KOPrefs::CategoryOnly) { QPopupMenu *assignMenu = new QPopupMenu(menu); assignMenu->insertItem(i18n("&Assign Color"), this, SLOT(assignColor())); if(item->resourceColor().isValid()) assignMenu->insertItem(i18n("&Disable Color"), this, SLOT(disableColor())); menu->insertItem(i18n("Resources Colors"), assignMenu); } menu->insertItem(i18n("&Edit..."), this, SLOT(editResource())); menu->insertItem(i18n("&Remove"), this, SLOT(removeResource())); if(item->resource() != manager->standardResource()) { menu->insertSeparator(); menu->insertItem(i18n("Use as &Default Calendar"), this, SLOT(setStandard())); } menu->insertSeparator(); } menu->insertItem(i18n("&Add..."), this, SLOT(addResource())); menu->popup(pos); }
void ResourcesContainer::AssimilateResources(ResourcesContainer &container) { // Resistance is futile! ;-) int32 newCount = container.CountResources(); for (int32 i = 0; i < newCount; i++) { ResourceItem *item = container.ResourceAt(i); if (item->IsLoaded()) AddResource(item); else { // That should not happen. // Delete the item to have a consistent behavior. delete item; } } container.fResources.MakeEmpty(); container.SetModified(true); SetModified(true); }
// Gets information about a resource identified by byPointer. bool BResources::GetResourceInfo(const void* byPointer, type_code* typeFound, int32* idFound, size_t* lengthFound, const char** nameFound) { ResourceItem* item = NULL; if (InitCheck() == B_OK) item = fContainer->ResourceAt(fContainer->IndexOf(byPointer)); if (item) { if (typeFound) *typeFound = item->Type(); if (idFound) *idFound = item->ID(); if (nameFound) *nameFound = item->Name(); if (lengthFound) *lengthFound = item->DataSize(); } return item; }
void ProjectPane::Data::OnItemDoubleClicked(QListWidgetItem* listitem) { ResourceItem* item = dynamic_cast<ResourceItem*>(listitem); if(item) { AVResource* res = item->GetResource(); if(res) { syBitmap bitmap; if(bitmap.LoadFromFile(res->m_Filename)) { syBitmap bitmap2(400,400,vcfRGB32); // bitmap2.ResampleFrom(&bitmap); bitmap2.ResampleFrom(&bitmap); BitmapDialog* dialog = new BitmapDialog(); dialog->setWindowTitle(res->m_RelativeFilename); dialog->LoadBitmap(&bitmap2); dialog->setModal(true); dialog->show(); } } } }
// _ReadIndexEntry bool ResourceFile::_ReadIndexEntry(int32 index, uint32 tableOffset, bool peekAhead) { bool result = true; resource_index_entry entry; // read one entry off_t entryOffset = tableOffset + index * kResourceIndexEntrySize; read_exactly(fFile, entryOffset, &entry, kResourceIndexEntrySize, "Failed to read a resource index entry."); // check, if the end is reached early if (result && check_pattern(entryOffset, &entry, kResourceIndexEntrySize / 4, fHostEndianess)) { if (!peekAhead) { Warnings::AddCurrentWarning("Unexpected end of resource index " "table at index: %ld (/%ld).", index + 1, fResourceCount); } result = false; } uint32 offset = _GetUInt32(entry.rie_offset); uint32 size = _GetUInt32(entry.rie_size); // check the location if (result && offset + size > fFileSize) { if (peekAhead) { Warnings::AddCurrentWarning("Invalid data after resource index " "table."); } else { throw Exception("Invalid resource index entry: index: %ld, " "offset: %lu (%lx), size: %lu (%lx).", index + 1, offset, offset, size, size); } result = false; } // add the entry if (result) { ResourceItem* item = new ResourceItem; item->SetLocation(offset, size); AddItem(item, index); } return result; }
void ResourceView::assignColor() { ResourceItem *item = currentItem(); if(!item) return; // A color without initialized is a color invalid QColor myColor; KCal::ResourceCalendar *cal = item->resource(); QString identifier = cal->identifier(); if(item->isSubresource()) identifier = item->resourceIdentifier(); QColor defaultColor = *KOPrefs::instance()->resourceColor(identifier); int result = KColorDialog::getColor(myColor, defaultColor); if(result == KColorDialog::Accepted) { KOPrefs::instance()->setResourceColor(identifier, myColor); item->setResourceColor(myColor); item->update(); emitResourcesChanged(); } }
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::removeResource() { ResourceItem *item = currentItem(); if ( !item ) return; const QString warningMsg = item->isSubresource() ? i18n("<qt>Do you really want to remove the subresource <b>%1</b>? " "Note that its contents will be completely deleted. This " "operation cannot be undone. </qt>").arg( item->text( 0 ) ) : i18n("<qt>Do you really want to remove the resource <b>%1</b>?</qt>").arg( item->text( 0 ) ); int km = KMessageBox::warningContinueCancel( this, warningMsg, "", KGuiItem( i18n("&Remove" ), "editdelete") ); if ( km == KMessageBox::Cancel ) return; // Don't be so restricitve #if 0 if ( item->resource() == mCalendar->resourceManager()->standardResource() ) { KMessageBox::sorry( this, i18n( "You cannot delete your standard resource." ) ); return; } #endif if ( item->isSubresource() ) { if ( !item->resource()->removeSubresource( item->resourceIdentifier() ) ) KMessageBox::sorry( this, i18n ("<qt>Failed to remove the subresource <b>%1</b>. The " "reason could be that it is a built-in one which cannot " "be removed, or that the removal of the underlying storage " "folder failed.</qt>").arg( item->resource()->name() ) ); return; } else { mCalendar->resourceManager()->remove( item->resource() ); } mListView->takeItem( item ); delete item; updateResourceList(); emit resourcesChanged(); }
// Loads all resources of the specified type into memory. status_t BResources::PreloadResourceType(type_code type) { status_t error = InitCheck(); if (error == B_OK && fResourceFile) { if (type == 0) error = fResourceFile->ReadResources(*fContainer); else { int32 count = fContainer->CountResources(); int32 errorCount = 0; for (int32 i = 0; i < count; i++) { ResourceItem *resource = fContainer->ResourceAt(i); if (resource->Type() == type) { if (fResourceFile->ReadResource(*resource) != B_OK) errorCount++; } } error = -errorCount; } } return error; }