From 0fe71063d5e7519ac1d7e0afbca8ee591b8e6ccc Mon Sep 17 00:00:00 2001
From: Sebastião Guerra <sebastiao.luiz.guerra@gmail.com>
Date: Sat, 5 Sep 2026 07:17:37 -0300
Subject: Fix archive format selection in Ark save dialog
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Sebastião Guerra <sebastiao.luiz.guerra@gmail.com>
---
 ark/CMakeLists.txt    |   2 +-
 ark/Makefile.am       |   2 +-
 ark/arkfiledialog.cpp | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++
 ark/arkfiledialog.h   |  50 ++++++++++++++
 ark/mainwindow.cpp    |  58 ++++------------
 5 files changed, 248 insertions(+), 48 deletions(-)
 create mode 100644 ark/arkfiledialog.cpp
 create mode 100644 ark/arkfiledialog.h

diff --git a/ark/CMakeLists.txt b/ark/CMakeLists.txt
index e275373e..f1140f76 100644
--- a/ark/CMakeLists.txt
+++ b/ark/CMakeLists.txt
@@ -34,7 +34,7 @@ tde_add_library( ark_common STATIC_PIC
 ##### ark (tdeinit) #############################
 
 tde_add_tdeinit_executable( ark AUTOMOC
-  SOURCES main.cpp arkapp.cpp mainwindow.cpp
+  SOURCES main.cpp arkapp.cpp mainwindow.cpp arkfiledialog.cpp
   LINK ark_common-static tdehtml-shared
 )
 
diff --git a/ark/Makefile.am b/ark/Makefile.am
index ee4ca6df..ff0e29da 100644
--- a/ark/Makefile.am
+++ b/ark/Makefile.am
@@ -8,7 +8,7 @@ bin_PROGRAMS =
 tdeinit_LTLIBRARIES = ark.la
 lib_LTLIBRARIES =
 
-ark_la_SOURCES = main.cpp arkapp.cpp mainwindow.cpp
+ark_la_SOURCES = main.cpp arkapp.cpp mainwindow.cpp arkfiledialog.cpp
 ark_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN) $(KDE_PLUGIN)
 ark_la_LIBADD = $(LIB_KDED) -lDCOP $(LIB_TDEHTML) $(LIB_TDEIO) $(LIB_TDEUI) $(LIB_TDECORE) $(LIB_TQT)  libark_common.la $(LIB_TDEPARTS)
 ark_la_COMPILE_FIRST = settings.h
diff --git a/ark/arkfiledialog.cpp b/ark/arkfiledialog.cpp
new file mode 100644
index 00000000..64128ebe
--- /dev/null
+++ b/ark/arkfiledialog.cpp
@@ -0,0 +1,184 @@
+/*
+
+ ark -- archiver for the KDE project
+
+ Copyright (C) 2002-2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
+ Copyright (C) 2003: Helio Chissini de Castro <helio@conectiva.com>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+*/
+
+#include <memory>
+
+#include <tqcheckbox.h>
+#include <tqlabel.h>
+#include <tqlayout.h>
+#include <tqobjectlist.h>
+
+#include <kcombobox.h>
+#include <tdediroperator.h>
+#include <tdefilefiltercombo.h>
+#include <tdelocale.h>
+
+#include "archiveformatinfo.h"
+#include "arkfiledialog.h"
+
+ArkFileDialog::ArkFileDialog( const TQString &startDir, TQWidget *parent, const char *name,
+        bool modal, bool addOnly, const TQString &suggestedName )
+    : KFileDialog( startDir, initialFilter( addOnly, suggestedName ), parent, name, modal,
+            createCustomWidget( parent, addOnly, suggestedName ) ),
+      m_filterFilesCheckBox( nullptr ), m_openAsCombo( nullptr )
+{
+    m_filterFilesCheckBox =
+            static_cast<TQCheckBox *>( child( "arkFilterFilesCheckBox" ) );
+    m_openAsCombo = static_cast<KComboBox *>( child( "arkOpenAsCombo" ) );
+
+    setOperationMode( addOnly ? KFileDialog::Saving : KFileDialog::Opening );
+
+    if ( addOnly )
+    {
+        TQString defaultMimeType = "application/x-tgz";
+
+        if ( !suggestedName.isEmpty() )
+        {
+            const TQString suggestedMimeType =
+                    KMimeType::findByPath( suggestedName, 0, true )->name();
+            if ( ArchiveFormatInfo::self()->archTypeForMimeType( suggestedMimeType ) !=
+                    UNKNOWN_FORMAT )
+            {
+                defaultMimeType = suggestedMimeType;
+            }
+        }
+
+        setMimeFilter( ArchiveFormatInfo::self()->supportedMimeTypes(), defaultMimeType );
+        setFilterLabelText( i18n( "Save &as:" ) );
+        setFileListFilterEnabled( false );
+
+        connect( this, TQ_SIGNAL( filterChanged( const TQString & ) ), this,
+                TQ_SLOT( slotArchiveFormatChanged( const TQString & ) ) );
+        if ( m_filterFilesCheckBox )
+        {
+            connect( m_filterFilesCheckBox, TQ_SIGNAL( toggled( bool ) ), this,
+                    TQ_SLOT( slotArchiveFilterToggled( bool ) ) );
+        }
+    }
+}
+
+TQString ArkFileDialog::openAsMimeType() const
+{
+    if ( m_openAsCombo && m_openAsCombo->currentItem() )
+    {
+        return ArchiveFormatInfo::self()->mimeTypeForDescription(
+                m_openAsCombo->currentText() );
+    }
+
+    return TQString::null;
+}
+
+void ArkFileDialog::slotArchiveFormatChanged( const TQString &filter )
+{
+    if ( !filterFilesBySelectedExtension() )
+    {
+        setFileListFilterEnabled( false );
+    }
+}
+
+void ArkFileDialog::slotArchiveFilterToggled( bool enabled )
+{
+    setFileListFilterEnabled( enabled );
+}
+
+bool ArkFileDialog::filterFilesBySelectedExtension() const
+{
+    return m_filterFilesCheckBox && m_filterFilesCheckBox->isChecked();
+}
+
+void ArkFileDialog::setFilterLabelText( const TQString &text )
+{
+    std::unique_ptr<TQObjectList> labels( queryList( "TQLabel" ) );
+    if ( !labels )
+    {
+        return;
+    }
+
+    TQObjectListIt it( *labels );
+    for ( ; it.current(); ++it )
+    {
+        TQLabel *label = static_cast<TQLabel *>( it.current() );
+        if ( label->buddy() == filterWidget )
+        {
+            label->setText( text );
+            break;
+        }
+    }
+}
+
+void ArkFileDialog::setFileListFilterEnabled( bool enabled )
+{
+    if ( enabled )
+    {
+        KFileDialog::slotFilterChanged();
+    }
+    else
+    {
+        ops->clearFilter();
+        ops->updateDir();
+    }
+}
+
+TQString ArkFileDialog::initialFilter( bool addOnly, const TQString &suggestedName )
+{
+    if ( addOnly || !suggestedName.isEmpty() )
+    {
+        return TQString::null;
+    }
+
+    return ArchiveFormatInfo::self()->filter();
+}
+
+TQWidget* ArkFileDialog::createCustomWidget( TQWidget *parent, bool addOnly,
+        const TQString &suggestedName )
+{
+    TQWidget *widget = new TQWidget( parent );
+    TQHBoxLayout *layout = new TQHBoxLayout( widget );
+
+    if ( addOnly )
+    {
+        TQCheckBox *checkBox = new TQCheckBox(
+                i18n( "&Filter files by selected extension" ),
+                widget, "arkFilterFilesCheckBox" );
+        layout->addWidget( checkBox );
+    }
+    else
+    {
+        TQLabel *label = new TQLabel( widget );
+        label->setText( i18n( "Open &as:" ) );
+        label->adjustSize();
+
+        KComboBox *combo = new KComboBox( widget, "arkOpenAsCombo" );
+        TQStringList list = ArchiveFormatInfo::self()->allDescriptions();
+        list.sort();
+        list.prepend( i18n( "Autodetect (default)" ) );
+        combo->insertStringList( list );
+
+        if ( !suggestedName.isEmpty() )
+        {
+            const TQString mimeType =
+                    KMimeType::findByPath( suggestedName, 0, true )->name();
+            combo->setCurrentItem( list.findIndex(
+                    ArchiveFormatInfo::self()->descriptionForMimeType( mimeType ) ) );
+        }
+
+        label->setBuddy( combo );
+        layout->addWidget( label );
+        layout->addWidget( combo, 1 );
+    }
+
+    return widget;
+}
+
+#include "arkfiledialog.moc"
diff --git a/ark/arkfiledialog.h b/ark/arkfiledialog.h
new file mode 100644
index 00000000..a714d1c0
--- /dev/null
+++ b/ark/arkfiledialog.h
@@ -0,0 +1,50 @@
+/*
+
+ ark -- archiver for the KDE project
+
+ Copyright (C) 2002-2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
+ Copyright (C) 2003: Helio Chissini de Castro <helio@conectiva.com>
+
+ This program is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License
+ as published by the Free Software Foundation; either version 2
+ of the License, or (at your option) any later version.
+
+*/
+
+#ifndef ARKFILEDIALOG_H
+#define ARKFILEDIALOG_H
+
+#include <tdefiledialog.h>
+
+class KComboBox;
+class TQCheckBox;
+
+class ArkFileDialog : public KFileDialog
+{
+    TQ_OBJECT
+
+public:
+    ArkFileDialog( const TQString &startDir, TQWidget *parent, const char *name, bool modal,
+            bool addOnly, const TQString &suggestedName );
+
+    TQString openAsMimeType() const;
+
+private slots:
+    void slotArchiveFormatChanged( const TQString &filter );
+    void slotArchiveFilterToggled( bool enabled );
+
+private:
+    bool filterFilesBySelectedExtension() const;
+    void setFilterLabelText( const TQString &text );
+    void setFileListFilterEnabled( bool enabled );
+
+    static TQString initialFilter( bool addOnly, const TQString &suggestedName );
+    static TQWidget* createCustomWidget( TQWidget *parent, bool addOnly,
+            const TQString &suggestedName );
+
+    TQCheckBox *m_filterFilesCheckBox;
+    KComboBox *m_openAsCombo;
+};
+
+#endif /* ARKFILEDIALOG_H */
diff --git a/ark/mainwindow.cpp b/ark/mainwindow.cpp
index 33f54ffc..ee233a34 100644
--- a/ark/mainwindow.cpp
+++ b/ark/mainwindow.cpp
@@ -22,7 +22,6 @@
 */
 
 // QT includes
-#include <tqlayout.h>
 
 // KDE includes
 #include <kdebug.h>
@@ -35,7 +34,6 @@
 #include <tdeparts/componentfactory.h>
 #include <tdeparts/browserextension.h>
 #include <kkeydialog.h>
-#include <kcombobox.h>
 #include <tdeio/netaccess.h>
 #include <tdeaccel.h>
 
@@ -43,6 +41,7 @@
 #include "arkapp.h"
 #include "settings.h"
 #include "archiveformatinfo.h"
+#include "arkfiledialog.h"
 #include "arkwidget.h"
 
 MainWindow::MainWindow( TQWidget * /*parent*/, const char *name )
@@ -240,64 +239,31 @@ MainWindow::openURL( const KURL & url, bool tempFile )
 }
 
 KURL
-MainWindow::getOpenURL( bool addOnly, const TQString & caption,
-                               const TQString & startDir, const TQString & suggestedName )
+MainWindow::getOpenURL( bool addOnly, const TQString & caption, const TQString & startDir,
+        const TQString & suggestedName )
 {
     kdDebug( 1601 ) << "startDir is: " << startDir << endl;
-    TQWidget * forceFormatWidget = new TQWidget( this );
-    TQHBoxLayout * l = new TQHBoxLayout( forceFormatWidget );
-
-    TQLabel * label = new TQLabel( forceFormatWidget );
-    label->setText( i18n( "Open &as:" ) );
-    label->adjustSize();
-
-    KComboBox * combo = new KComboBox( forceFormatWidget );
-
-    TQStringList list;
-    list = ArchiveFormatInfo::self()->allDescriptions();
-    list.sort();
-    list.prepend( i18n( "Autodetect (default)" ) );
-    combo->insertStringList( list );
-
-    TQString filter = ArchiveFormatInfo::self()->filter();
-    if ( !suggestedName.isEmpty() )
-    {
-        filter = TQString();
-        combo->setCurrentItem( list.findIndex( ArchiveFormatInfo::self()->descriptionForMimeType(
-                                 KMimeType::findByPath( suggestedName, 0, true )->name() ) ) );
-    }
-
-    label->setBuddy( combo );
-
-    l->addWidget( label );
-    l->addWidget( combo, 1 );
 
     TQString dir;
     if ( addOnly )
+    {
         dir = startDir;
+    }
     else
+    {
         dir = ":ArkOpenDir";
+    }
 
-    KFileDialog dlg( dir, filter, this, "filedialog", true, forceFormatWidget );
-    dlg.setOperationMode( addOnly ? KFileDialog::Saving
-                                  : KFileDialog::Opening );
+    ArkFileDialog dlg( dir, this, "filedialog", true, addOnly, suggestedName );
 
-    dlg.setCaption( addOnly ? caption : i18n("Open") );
-    dlg.setMode( addOnly ? ( KFile::File | KFile::ExistingOnly )
-                                  :  KFile::File );
+    dlg.setCaption( addOnly ? caption : i18n( "Open" ) );
+    dlg.setMode( addOnly ? ( KFile::File | KFile::ExistingOnly ) : KFile::File );
     dlg.setSelection( suggestedName );
-
     dlg.exec();
 
-    KURL url;
-    url = dlg.selectedURL();
-
-    if ( combo->currentItem() !=0 ) // i.e. != "Autodetect"
-        m_widget->setOpenAsMimeType(
-            ArchiveFormatInfo::self()->mimeTypeForDescription( combo->currentText() ) );
-    else
-        m_widget->setOpenAsMimeType( TQString() );
+    KURL url = dlg.selectedURL();
 
+    m_widget->setOpenAsMimeType( dlg.openAsMimeType() );
     return url;
 }
 
-- 
cgit v1.2.3

