summaryrefslogtreecommitdiffstats
path: root/src/common/gui/purl_gui.cpp
blob: 3f5a742ea22596d765992dabf9252431e00f43f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/***************************************************************************
 *   Copyright (C) 2006 Nicolas Hadacek <hadacek@kde.org>                  *
 *                                                                         *
 *   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 "purl_gui.h"

#include <tqlayout.h>
#include <kiconloader.h>
#include <kpushbutton.h>
#include <krun.h>
#include <tdefiledialog.h>
#include <kdirselectdialog.h>

#include "misc_gui.h"

//-----------------------------------------------------------------------------
PURL::Url PURL::getOpenUrl(const TQString &startDir, const TQString &filter,
                           TQWidget *widget, const TQString &caption)
{
  return KFileDialog::getOpenURL(startDir, filter, widget, caption);
}

PURL::UrlList PURL::getOpenUrls(const TQString &startDir, const TQString &filter,
                                TQWidget *widget, const TQString &caption)
{
  return KFileDialog::getOpenURLs(startDir, filter, widget, caption);
}

PURL::Url PURL::getSaveUrl(const TQString &startDir, const TQString &filter,
                           TQWidget *widget, const TQString &caption,
                           SaveAction action)
{
  Url url = KFileDialog::getSaveURL(startDir, filter, widget, caption);
  if ( url.isEmpty() ) return Url();
  switch (action) {
    case NoSaveAction: break;
    case AskOverwrite:
      if ( url.exists() ) {
        if ( !MessageBox::askContinue(i18n("File \"%1\" already exists. Overwrite ?").arg(url.pretty())) ) return Url();
      }
      break;
    case CancelIfExists:
      if ( url.exists() ) return Url();
      break;
  }
  return url;
}

PURL::Directory PURL::getExistingDirectory(const TQString &startDir, TQWidget *widget,
                                           const TQString &caption)
{
  KURL kurl = KDirSelectDialog::selectDirectory(startDir, false, widget, caption);
  if ( kurl.isEmpty() ) return Directory();
  return Directory(kurl.path(1));
}

TQPixmap PURL::icon(FileType type)
{
  if (type.data().xpm_icon) return TQPixmap(type.data().xpm_icon);
  if ( hasMimetype(type) ) return KMimeType::mimeType(type.data().mimetype)->pixmap(TDEIcon::Small);
  return TQPixmap();
}

bool PURL::hasMimetype(FileType type)
{
  if ( type.data().mimetype==0 ) return false;
  KMimeType::Ptr ptr = KMimeType::mimeType(type.data().mimetype);
  return ( ptr!=KMimeType::defaultMimeTypePtr() );
}

//-----------------------------------------------------------------------------
PURL::Label::Label(const TQString &url, const TQString &text,
                   TQWidget *parent, const char *name)
  : KURLLabel(url, text, parent, name)
{
  connect(this, TQT_SIGNAL(leftClickedURL()), TQT_SLOT(urlClickedSlot()));
}

void PURL::Label::urlClickedSlot()
{
  (void)new KRun(url());
}

//-----------------------------------------------------------------------------
PURL::BaseWidget::BaseWidget(TQWidget *parent, const char *name)
  : TQWidget(parent, name)
{
  init();
}

PURL::BaseWidget::BaseWidget(const TQString &defaultDir, TQWidget *parent, const char *name)
  : TQWidget(parent, name), _defaultDir(defaultDir)
{
  init();
}

void PURL::BaseWidget::init()
{
  TQHBoxLayout *top = new TQHBoxLayout(this, 0, 10);

  _edit = new KLineEdit(this);
  connect(_edit, TQT_SIGNAL(textChanged(const TQString &)), TQT_SIGNAL(changed()));
  top->addWidget(_edit);
  TDEIconLoader loader;
  TQIconSet iconset = loader.loadIcon("fileopen", TDEIcon::Toolbar);
  TQPushButton *button = new  KPushButton(iconset, TQString(), this);
  connect(button, TQT_SIGNAL(clicked()), TQT_SLOT(buttonClicked()));
  top->addWidget(button);
}

//----------------------------------------------------------------------------
void PURL::DirectoryWidget::buttonClicked()
{
  Directory dir = getExistingDirectory(_defaultDir, this, i18n("Select Directory"));
  if ( dir.isEmpty() ) return;
  _edit->setText(dir.path());
  emit changed();
}

//----------------------------------------------------------------------------
PURL::DirectoriesWidget::DirectoriesWidget(const TQString &title, TQWidget *parent, const char *name)
  : TQVGroupBox(title, parent, name)
{
  init(TQString());
}

PURL::DirectoriesWidget::DirectoriesWidget(const TQString &title, const TQString &defaultDir, TQWidget *parent, const char *name)
  : TQVGroupBox(title, parent, name)
{
  init(defaultDir);
}

void PURL::DirectoriesWidget::init(const TQString &defaultDir)
{
  DirectoryWidget *edit = new DirectoryWidget(defaultDir);
  _editListBox = new EditListBox(1, edit, edit->lineEdit(), this, "directories_editlistbox");
  connect(_editListBox, TQT_SIGNAL(changed()), TQT_SIGNAL(changed()));
}

//----------------------------------------------------------------------------
void PURL::UrlWidget::buttonClicked()
{
  Url url = getOpenUrl(_defaultDir, _filter, this, i18n("Select File"));
  if ( url.isEmpty() ) return;
  _edit->setText(url.filepath());
  emit changed();
}