summaryrefslogtreecommitdiffstats
path: root/kshowmail/kcmconfigs/mailboxwizard.cpp
blob: a64f1ad67815137bf8c1f53a18bde1a2fde074f6 (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
//
// C++ Implementation: mailboxwizard
//
// Description:
//
//
// Author: Ulrich Weigelt <ulrich.weigelt@gmx.de>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "mailboxwizard.h"

MailBoxWizard::MailBoxWizard( TQWidget* parent, const char* name )
 : TQWizard( parent, name, true )
{

  //this is page one
  //in this the user can chosse his mail directory
  //----------------------------------------------
  TQWidget* page1 = new TQWidget( this, "page1" );
  TQHBoxLayout* layMain1 = new TQHBoxLayout( page1, 0, 10 );

  txtMailDir = new KLineEdit( page1, "txtMailDir" );
  layMain1->addWidget( txtMailDir );

  btnMailDir = new KPushButton( KGuiItem( TQString(), TQString( "folder" ), i18n( "Press to choose the mail directory" ), i18n( "Press to choose the mail directory" ) ), page1, "btnMailDir" );
  btnMailDir->setSizePolicy( TQSizePolicy::Fixed, TQSizePolicy::Fixed );
  layMain1->addWidget( btnMailDir );
  connect( btnMailDir, TQ_SIGNAL( clicked() ), this, TQ_SLOT( slotOpenDirDialog() ) );

  title1 = i18n( "Please choose the path to the mailboxes.\nKShowmail supports only MailDir boxes." );
  addPage( page1, title1 );


  //this is page two
  //in this the user can choose the mailbox
  //---------------------------------------
  TQWidget* page2 = new TQWidget( this, "page2" );
  TQHBoxLayout* layMain2 = new TQHBoxLayout( page2, 0, 10 );

  lstMailboxes = new TDEListView( page2, "lstMailboxes" );
  lstMailboxes->addColumn( "Mailbox" );
  lstMailboxes->setRootIsDecorated( true );
  layMain2->addWidget( lstMailboxes );

  title2 = i18n( "Please choose the mailbox" );
  addPage( page2, title2 );
  setFinishEnabled( page2, true );

  connect( this, TQ_SIGNAL( selected( const TQString& ) ), this, TQ_SLOT( slotPageChanged( const TQString& ) ) );
}


MailBoxWizard::~MailBoxWizard()
{
}

void MailBoxWizard::slotOpenDirDialog( )
{
  //save old path
  TQString oldPath = txtMailDir->text();

  //get new path
  TQString path = KFileDialog::getExistingDirectory( oldPath, this, i18n( "Choose the mailbox directory") );

  //put new or old path in the edit line
  if( path == TQString::null )
    txtMailDir->setText( oldPath );
  else
    txtMailDir->setText( path );
}

void MailBoxWizard::slotPageChanged( const TQString& pageTitle )
{
  //just we looking for mailboxes if the page 2 was opened
  if( pageTitle == title2 )
  {
    //clear all entries
    lstMailboxes->clear();

    //create an directory object to browse the given directory
    TQDir mailDir( txtMailDir->text() );
    if( mailDir.isReadable() )  //is the dir readable?
    {
      //get a list of all entries in this directory
      const TQStringList entries = mailDir.entryList( TQDir::Dirs | TQDir::Readable | TQDir::Writable | TQDir::Hidden, TQDir::Name | TQDir::IgnoreCase | TQDir::LocaleAware );

      for( TQStringList::const_iterator it = entries.begin(); it != entries.end(); ++it )
      {
        //add an entry to the mailbox list
        TQDir newMailDir( mailDir );
        newMailDir.cd( (*it) );
        if( (*it) != ".." && (*it) != "." && isMailDir( newMailDir ) )
          addMailBoxListItem( *it, mailDir );
      }
    }
  }

}

bool MailBoxWizard::isMailDir( const TQDir & path )
{
  //get a list of all subdirectories in this directory
  const TQStringList entries = path.entryList( TQDir::Dirs | TQDir::Readable | TQDir::Writable | TQDir::Hidden, TQDir::Name | TQDir::IgnoreCase | TQDir::LocaleAware );

  //a maildir folder must contains the folders "cur", "new" and "tmp"
  bool curFound = false;
  bool newFound = false;
  bool tmpFound = false;

  //iterate over all directories and look for the three necessary dirs
  TQStringList::const_iterator it = entries.begin();
  while( it != entries.end() && !( curFound && newFound && tmpFound ) )
  {
    if( *it == "tmp" )
      tmpFound = true;
    else if( *it == "cur" )
      curFound = true;
    else if( *it == "new" )
      newFound = true;

    ++it;
  }

  return curFound && newFound && tmpFound;
}

void MailBoxWizard::addMailBoxListItem( TQString boxname, TQDir path )
{
  //translate some default mailboxes
  TQString boxnameTrans;
  if( boxname.lower() == "inbox" )
    boxnameTrans = i18n( "Inbox" );
  else if( boxname.lower() == "outbox" )
    boxnameTrans = i18n( "Outbox" );
  else if( boxname.lower() == "drafts" )
    boxnameTrans = i18n( "Drafts" );
  else if( boxname.lower() == "sent-mail" )
    boxnameTrans = i18n( "sent-mail" );
  else if( boxname.lower() == "trash" )
    boxnameTrans = i18n( "Trash" );
  else
    boxnameTrans = boxname;

  //create item
  MailBoxWizardListItem* newItem;
  newItem = new MailBoxWizardListItem( lstMailboxes, boxnameTrans, path.absPath() + "/" + boxname + "/" );


}

TQString MailBoxWizard::getPath( )
{
  MailBoxWizardListItem* item = (MailBoxWizardListItem*)lstMailboxes->selectedItem();

  TQString path = TQString::null;
  if( item != NULL )
    path = item->getPath();

  return path;
}

#include "mailboxwizard.moc"