/***************************************************************************
    Private helper class(es) for the profile manager.
                             -------------------
    begin                : Mi Aug 12 2014
    copyright            : (C) 2014-2019 by Alexander Reinholdt
    email                : alexander.reinholdt@kdemail.net
 ***************************************************************************/

/***************************************************************************
 *   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.                                   *
 *                                                                         *
 *   This program 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     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc., 51 Franklin Street, Suite 500, Boston,*
 *   MA 02110-1335, USA                                                    *
 ***************************************************************************/

// application specific includes
#include "smb4kprofilemanager_p.h"
#include "smb4ksettings.h"

// Qt includes
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLabel>
#include <QDialogButtonBox>
#include <QPixmap>
#include <QWindow>

// KDE includes
#define TRANSLATION_DOMAIN "smb4k-core"
#include <KI18n/KLocalizedString>
#include <KIconThemes/KIconLoader>
#include <KConfigGui/KWindowConfig>


Smb4KProfileMigrationDialog::Smb4KProfileMigrationDialog(const QStringList& from, const QStringList& to, QWidget* parent)
: QDialog(parent), m_from_list(from), m_to_list(to)
{
  //
  // Set the window title
  // 
  setWindowTitle(i18n("Profile Migration Assistant"));
  
  //
  // Setup the view
  // 
  setupView();
  
  //
  // Set the dialog size
  // 
  create();

  KConfigGroup group(Smb4KSettings::self()->config(), "ProfileMigrationDialog");
  QSize dialogSize;
  
  if (group.exists())
  {
    KWindowConfig::restoreWindowSize(windowHandle(), group);
    dialogSize = windowHandle()->size();
  }
  else
  {
    dialogSize = sizeHint();
  }
  
  resize(dialogSize); // workaround for QTBUG-40584
}


Smb4KProfileMigrationDialog::~Smb4KProfileMigrationDialog()
{
}


void Smb4KProfileMigrationDialog::setupView()
{
  //
  // The layout
  // 
  QVBoxLayout *layout = new QVBoxLayout(this);
  
  // 
  // The description
  // 
  QWidget *description = new QWidget(this);
  QHBoxLayout *descriptionLayout = new QHBoxLayout(description);
  descriptionLayout->setContentsMargins(0, 0, 0, 0);

  QLabel *pixmap = new QLabel(description);
  QPixmap pix = KDE::icon("format-list-unordered").pixmap(KIconLoader::SizeHuge);
  pixmap->setPixmap(pix);
  pixmap->setAlignment(Qt::AlignBottom);

  QLabel *label = new QLabel(i18n("Migrate all relevant settings of one profile to another."));
  label->setWordWrap(true);
  label->setAlignment(Qt::AlignBottom);

  descriptionLayout->addWidget(pixmap, 0);
  descriptionLayout->addWidget(label, Qt::AlignBottom);
  
  //
  // The input widgets
  // 
  QWidget *editors = new QWidget(this);
  QGridLayout *editorsLayout = new QGridLayout(editors);
  editorsLayout->setSpacing(5);
  editorsLayout->setContentsMargins(0, 0, 0, 0);
  editorsLayout->setColumnStretch(0, 0);
  editorsLayout->setColumnStretch(1, 1);
  
  QLabel *from = new QLabel(i18n("Old Profile:"), editors);
  editorsLayout->addWidget(from, 0, 0);
  
  m_from_box = new KComboBox(editors);
  
  if (m_from_list.size() == 1 && m_from_list.first().isEmpty())
  {
    m_from_box->addItem(i18n("<Default Profile>"));
  }
  else
  {
    if (m_to_list.size() == 1 && m_to_list.first().isEmpty())
    {
      m_from_box->addItem(i18n("<All Profiles>"));
    }
    else
    {
      m_from_box->addItems(m_from_list);
    }
  }
  
  editorsLayout->addWidget(m_from_box, 0, 1);
  
  QLabel *to = new QLabel(i18n("New Profile:"), editors);
  editorsLayout->addWidget(to, 1, 0);
  
  m_to_box = new KComboBox(editors);
  
  if (m_to_list.size() == 1 && m_to_list.first().isEmpty())
  {
    m_to_box->addItem(i18n("<Default Profile>"));
  }
  else
  {
    m_to_box->addItems(m_to_list);
    m_to_box->setCurrentText(Smb4KProfileManager::self()->activeProfile());
  }
  
  editorsLayout->addWidget(m_to_box, 1, 1);
  
  QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
  m_ok_button = buttonBox->addButton(QDialogButtonBox::Ok);
  m_cancel_button = buttonBox->addButton(QDialogButtonBox::Cancel);
  
  m_ok_button->setShortcut(Qt::CTRL|Qt::Key_Return);
  m_cancel_button->setShortcut(Qt::Key_Escape);
  
  m_ok_button->setDefault(true);
  m_ok_button->setEnabled(!m_to_box->currentText().isEmpty());
  
  layout->addWidget(description, 0);
  layout->addWidget(editors, 0);
  layout->addWidget(buttonBox, 0);
  
  connect(m_ok_button, SIGNAL(clicked()), this, SLOT(slotOkClicked()));
  connect(m_cancel_button, SIGNAL(clicked()), this, SLOT(reject()));
}


QString Smb4KProfileMigrationDialog::from() const
{
  if (m_from_box->currentText() == i18n("<Default Profile>"))
  {
    return QString();
  }
  
  return m_from_box->currentText();
}


QString Smb4KProfileMigrationDialog::to() const
{
  if (m_to_box->currentText() == i18n("<Default Profile>"))
  {
    return QString();
  }
  
  return m_to_box->currentText();
}


void Smb4KProfileMigrationDialog::slotOkClicked()
{
  KConfigGroup group(Smb4KSettings::self()->config(), "ProfileMigrationDialog");
  KWindowConfig::saveWindowSize(windowHandle(), group);
  accept();
}

