summaryrefslogtreecommitdiffstats
path: root/kcontrol/filetypes/keditfiletype.cpp
blob: 19aa01a590fad1b2ddaad7962b8d5cc11b84aa15 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* This file is part of the KDE project
   Copyright (C) 2000 David Faure <faure@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License version 2 as published by the Free Software Foundation.

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/
#include "filetypedetails.h"
#include "typeslistitem.h"
#include "keditfiletype.h"

#include <tqfile.h>

#include <dcopclient.h>
#include <tdeapplication.h>
#include <tdeaboutdata.h>
#include <kdebug.h>
#include <tdecmdlineargs.h>
#include <tdesycoca.h>
#include <kstandarddirs.h>

#ifdef Q_WS_X11
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif

FileTypeDialog::FileTypeDialog( KMimeType::Ptr mime )
  : KDialogBase( 0L, 0, false, TQString::null, /* Help | */ Cancel | Apply | Ok,
                 Ok, false )
{
  init( mime, false );
}

FileTypeDialog::FileTypeDialog( KMimeType::Ptr mime, bool newItem )
  : KDialogBase( 0L, 0, false, TQString::null, /* Help | */ Cancel | Apply | Ok,
                 Ok, false )
{
  init( mime, newItem );
}

void FileTypeDialog::init( KMimeType::Ptr mime, bool newItem )
{
  m_details = new FileTypeDetails( this );
  TQListView * dummyListView = new TQListView( m_details );
  dummyListView->hide();
  m_item = new TypesListItem( dummyListView, mime, newItem );
  m_details->setTypeItem( m_item );

  // This code is very similar to kcdialog.cpp
  setMainWidget( m_details );
  connect(m_details, TQT_SIGNAL(changed(bool)), this, TQT_SLOT(clientChanged(bool)));
  // TODO setHelp()
  enableButton(Apply, false);

  connect( KSycoca::self(), TQT_SIGNAL( databaseChanged() ), TQT_SLOT( slotDatabaseChanged() ) );
}

void FileTypeDialog::save()
{
  if (m_item->isDirty()) {
    m_item->sync();
    KService::rebuildKSycoca(this);
  }
}

void FileTypeDialog::slotApply()
{
  save();
}

void FileTypeDialog::slotOk()
{
  save();
  accept();
}

void FileTypeDialog::clientChanged(bool state)
{
  // enable/disable buttons
  enableButton(User1, state);
  enableButton(Apply, state);
}

void FileTypeDialog::slotDatabaseChanged()
{
  if ( KSycoca::self()->isChanged( "mime" ) )
  {
      m_item->refresh();
  }
}

#include "keditfiletype.moc"

static TDECmdLineOptions options[] =
{
  { "parent <winid>", I18N_NOOP("Makes the dialog transient for the window specified by winid"), 0 },
  { "+mimetype",   I18N_NOOP("File type to edit (e.g. text/html)"), 0 },
  TDECmdLineLastOption
};

int main(int argc, char ** argv)
{
  TDELocale::setMainCatalogue("filetypes");
  TDEAboutData aboutData( "keditfiletype", I18N_NOOP("KEditFileType"), "1.0",
                        I18N_NOOP("TDE file type editor - simplified version for editing a single file type"),
                        TDEAboutData::License_GPL,
                        I18N_NOOP("(c) 2000, KDE developers") );
  aboutData.addAuthor("Preston Brown",0, "pbrown@kde.org");
  aboutData.addAuthor("David Faure",0, "faure@kde.org");

  TDECmdLineArgs::init( argc, argv, &aboutData );
  TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.
  TDEApplication app;
  TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

  if (args->count() == 0)
    TDECmdLineArgs::usage();

  TQString arg = args->arg(0);

  bool createType = arg.startsWith( "*" );

  KMimeType::Ptr mime;
  
  if ( createType ) {
    TQString mimeString = "application/x-kdeuser%1";
    TQString loc;
    int inc = 0;
    do {
      ++inc;
      loc = locateLocal( "mime", mimeString.arg( inc ) + ".desktop" );
    }
    while ( TQFile::exists( loc ) );

    TQStringList patterns;
    if ( arg.length() > 2 )
	patterns << arg.lower() << arg.upper();
    TQString comment;
    if ( arg.startsWith( "*." ) && arg.length() >= 3 ) {
	TQString type = arg.mid( 3 ).prepend( arg[2].upper() );
        comment = i18n( "%1 File" ).arg( type );
    }
    mime = new KMimeType( loc, mimeString.arg( inc ), TQString::null, comment, patterns );
  }
  else { 
    mime = KMimeType::mimeType( arg );
  if (!mime)
      kdFatal() << "Mimetype " << arg << " not found" << endl;
  }

  FileTypeDialog dlg( mime, createType );
#if defined Q_WS_X11
  if( args->isSet( "parent" )) {
    bool ok;
    long id = args->getOption("parent").toLong(&ok);
    if (ok)
      XSetTransientForHint( tqt_xdisplay(), dlg.winId(), id );
  }
#endif
  args->clear();
  if ( !createType )
  dlg.setCaption( i18n("Edit File Type %1").arg(mime->name()) );
  else {
    dlg.setCaption( i18n("Create New File Type %1").arg(mime->name()) );
    dlg.enableButton( KDialogBase::Apply, true );
  }
  app.setMainWidget( &dlg );
  dlg.show(); // non-modal

  return app.exec();
}