kaddressbook

main.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (C) 1999 Don Sanders <sanders@kde.org>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <stdlib.h>
25 #include <unistd.h>
26 
27 #include <tqstring.h>
28 
29 #include <tdeabc/stdaddressbook.h>
30 #include <tdeaboutdata.h>
31 #include <tdecmdlineargs.h>
32 #include <kcrash.h>
33 #include <kdebug.h>
34 #include <tdelocale.h>
35 #include <tdestartupinfo.h>
36 #include <tdeuniqueapplication.h>
37 #include <twin.h>
38 
39 #include "kaddressbookmain.h"
40 #include "kaddressbook_options.h"
41 #include "kabcore.h"
42 
43 class KAddressBookApp : public TDEUniqueApplication {
44  public:
45  KAddressBookApp() : mMainWin( 0 ), mDefaultIsOpen( false ) {}
46  ~KAddressBookApp() {}
47 
48  int newInstance();
49 
50  private:
51  KAddressBookMain *mMainWin;
52  bool mDefaultIsOpen;
53 };
54 
55 int KAddressBookApp::newInstance()
56 {
57  if ( isRestored() ) {
58  // There can only be one main window
59  if ( TDEMainWindow::canBeRestored( 1 ) ) {
60  mMainWin = new KAddressBookMain;
61  setMainWidget( mMainWin );
62  mMainWin->show();
63  mMainWin->restore( 1 );
64  }
65  } else {
66  TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
67 
68  if ( args->isSet( "editor-only" ) ) {
69  if ( !mMainWin ) {
70  mMainWin = new KAddressBookMain;
71  setMainWidget( mMainWin );
72  mMainWin->hide();
73  }
74  // otherwise, leave the window like it is (hidden or shown)
75  TDEStartupInfo::appStarted();
76  } else {
77  TQString file;
78  if ( args->isSet( "document" ) ) {
79  file = args->getOption( "document" );
80  }
81  if ( !( file.isEmpty() && mDefaultIsOpen ) ) {
82  if ( !mMainWin ) {
83  mMainWin = new KAddressBookMain( file );
84  setMainWidget( mMainWin );
85  mMainWin->show();
86  } else {
87  KAddressBookMain *m = new KAddressBookMain( file );
88  m->show();
89  }
90  if ( file.isEmpty() ) mDefaultIsOpen = true;
91  }
92  }
93 
94  mMainWin->handleCommandLine();
95  }
96 
97  // Handle startup notification and window activation
98  // We do it ourselves instead of calling TDEUniqueApplication::newInstance
99  // to avoid the show() call there.
100 #if defined TQ_WS_X11 && ! defined K_WS_TQTONLY
101  static bool firstInstance = true;
102 
103  if ( !firstInstance )
104  TDEStartupInfo::setNewStartupId( mMainWin, kapp->startupId() );
105 
106  firstInstance = false;
107 #endif
108 
109  return 0;
110 }
111 
112 int main( int argc, char *argv[] )
113 {
114  TDELocale::setMainCatalogue( "kaddressbook" );
115 
116  TDECmdLineArgs::init( argc, argv, KABCore::createAboutData() );
117  TDECmdLineArgs::addCmdLineOptions( kaddressbook_options );
118  TDEUniqueApplication::addCmdLineOptions();
119 
120  if ( !KAddressBookApp::start() )
121  return 0;
122 
123  KAddressBookApp app;
124  TDEGlobal::locale()->insertCatalogue( "libtdepim" );
125 
126  bool ret = app.exec();
127  while (TDEMainWindow::memberList->first())
128  delete TDEMainWindow::memberList->first();
129  return ret;
130 }
This class serves as the main window for KAddressBook.