From 29b0a49abbfb9b731f2fa60c72ca23a4c0fb8e49 Mon Sep 17 00:00:00 2001
From: Sebastião Guerra <sebastiao.luiz.guerra@gmail.com>
Date: Wed, 26 Aug 2026 20:30:20 -0300
Subject: KOrganizer: improve attachment handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Create new attachments as binary data and default them to inline storage.

Derive the attachment name from the selected file when no explicit name is provided.

Use KURL::fromPathOrURL() when handling paths so local filenames containing URL-special characters such as '#' are processed correctly.

Based on the attachment improvements from TDE/tdepim#97.

Co-authored-by: Roman Savochenko <roman@home.home>
Signed-off-by: Sebastião Guerra <sebastiao.luiz.guerra@gmail.com>
---
 korganizer/koeditorattachments.cpp | 28 ++++++++++------------------
 1 file changed, 10 insertions(+), 18 deletions(-)

diff --git a/korganizer/koeditorattachments.cpp b/korganizer/koeditorattachments.cpp
index 6bf22137..833dd99b 100644
--- a/korganizer/koeditorattachments.cpp
+++ b/korganizer/koeditorattachments.cpp
@@ -79,8 +79,7 @@ class AttachmentListItem : public TDEIconViewItem
       if ( att ) {
         mAttachment = new KCal::Attachment( *att );
       } else {
-        mAttachment = new KCal::Attachment( TQChar('\0') ); //use the non-uri constructor
-                                                    //as we want inline by default
+        mAttachment = new KCal::Attachment( nullptr );
       }
       readAttachment();
       setDragEnabled( true );
@@ -203,7 +202,7 @@ AttachmentEditDialog::AttachmentEditDialog( AttachmentListItem *item,
 
   mInline = new TQCheckBox( i18n( "Store attachment inline" ), topFrame );
   grid->addMultiCellWidget( mInline, 3, 3, 0, 2 );
-  mInline->setChecked( item->isBinary() );
+  mInline->setChecked( item->isBinary() || item->label().isEmpty() );
   TQToolTip::add( mInline, i18n( "Store the attachment file inside the calendar" ) );
   TQWhatsThis::add(
     mInline,
@@ -214,7 +213,7 @@ AttachmentEditDialog::AttachmentEditDialog( AttachmentListItem *item,
           "attachments that change often or may be moved (or removed) from "
           "their current location." ) );
 
-  if ( item->attachment()->isUri() || !item->attachment()->data() ) {
+  if ( item->attachment()->isUri() || item->label().isEmpty() || !item->attachment()->data() ) {
     label = new TQLabel( i18n( "Location:" ), topFrame );
     grid->addWidget( label, 4, 0 );
     mURLRequester = new KURLRequester( item->uri(), topFrame );
@@ -244,22 +243,15 @@ void AttachmentEditDialog::slotApply()
 {
   if ( !mLabelEdit->text().isEmpty() ) {
     mItem->setLabel( mLabelEdit->text() );
+  } else if ( mURLRequester && !mURLRequester->url().isEmpty() ) {
+    KURL url = KURL::fromPathOrURL( mURLRequester->url() );
+    mItem->setLabel( url.isLocalFile() ? url.fileName() : url.url() );
   } else {
-    if ( mURLRequester ) {
-      KURL url( mURLRequester->url() );
-      if ( url.isLocalFile() ) {
-        mItem->setLabel( url.fileName() );
-      } else {
-        mItem->setLabel( url.url() );
-      }
-    }
-  }
-  if ( mItem->label().isEmpty() ) {
     mItem->setLabel( i18n( "New attachment" ) );
   }
   mItem->setMimeType( mMimeType->name() );
   if ( mURLRequester ) {
-    KURL url( mURLRequester->url() );
+    KURL url = KURL::fromPathOrURL( mURLRequester->url() );
 
     TQString correctedUrl = mURLRequester->url();
     if ( !url.isValid() ) {
@@ -269,7 +261,7 @@ void AttachmentEditDialog::slotApply()
       // to the home directory (not pwd), so we must prepend home
 
       correctedUrl = TQDir::home().filePath( mURLRequester->url() );
-      url = KURL( correctedUrl );
+      url = KURL::fromPathOrURL( correctedUrl );
       if ( url.isValid() ) {
         urlSelected( correctedUrl );
         mItem->setMimeType( mMimeType->name() );
@@ -278,7 +270,7 @@ void AttachmentEditDialog::slotApply()
 
     if ( mInline->isChecked() ) {
       TQString tmpFile;
-      if ( TDEIO::NetAccess::download( correctedUrl, tmpFile, this ) ) {
+      if ( TDEIO::NetAccess::download( url, tmpFile, this ) ) {
         TQFile f( tmpFile );
         if ( !f.open( IO_ReadOnly ) ) {
           return;
@@ -307,7 +299,7 @@ void AttachmentEditDialog::urlChanged( const TQString &url )
 
 void AttachmentEditDialog::urlSelected( const TQString &url )
 {
-  KURL kurl( url );
+  KURL kurl = KURL::fromPathOrURL( url );
   mMimeType = KMimeType::findByURL( kurl );
   mTypeLabel->setText( mMimeType->comment() );
   mIcon->setPixmap( AttachmentListItem::icon( mMimeType, kurl.path() ) );
-- 
cgit v1.2.3

