home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / qt3_emx.zip / examples / network / networkprotocol / view.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-11  |  2.9 KB  |  92 lines

  1. /****************************************************************************
  2. ** $Id:  qt/view.cpp   3.0.0   edited Jun 1 18:44 $
  3. **
  4. ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
  5. **
  6. ** This file is part of an example program for Qt.  This example
  7. ** program may be used, distributed and modified without limitation.
  8. **
  9. *****************************************************************************/
  10.  
  11. #include "view.h"
  12.  
  13. #include <qlabel.h>
  14. #include <qpushbutton.h>
  15. #include <qmultilineedit.h>
  16. #include <qfiledialog.h>
  17.  
  18. View::View()
  19.     : QVBox()
  20. {
  21.     // setup the GUI
  22.     setSpacing( 5 );
  23.     setMargin( 5 );
  24.  
  25.     QLabel *l = new QLabel( this );
  26.     l->setAlignment( Qt::WordBreak ),
  27.     l->setText( tr( "The button below opens the QFileDialog and you "
  28.             "can choose a file then which is downloaded and "
  29.             "opened below then. You can use for that the <b>local "
  30.             "filesystem</b> using the file protocol, you can download "
  31.             "files from an <b>FTP</b> server using the ftp protocol and "
  32.             "you can download and open <b>USENET</b> articles using the "
  33.             "demo implementation of the nntp protocol of this "
  34.             "exmaple (<i>This implementation of the nntp protocol is a very "
  35.             "basic and incomplete one, so you need to connect to a news server "
  36.             "which allows reading without authentification</i>)\n"
  37.             "To open a file from the local filesystem, enter in the "
  38.             "path combobox of the file dialog a url starting with file "
  39.             "(like <u>file:/usr/bin</u>), to download something from an FTP "
  40.             "server, use something like <u>ftp://ftp.trolltech.com</u> as url, and "
  41.             "for downloading a news article start with an url like "
  42.             "<u>nntp://news.tu-graz.ac.at</u> " ) );
  43.     QPushButton *b = new QPushButton( tr( "Open a file..." ), this );
  44.     connect( b, SIGNAL( clicked() ),
  45.          this, SLOT( downloadFile() ) );
  46.  
  47.     fileView = new QMultiLineEdit( this );
  48.     fileView->setReadOnly( TRUE );
  49.  
  50.     // if new data comes in, display it
  51.     connect( &op, SIGNAL( data( const QByteArray &, QNetworkOperation * ) ),
  52.          this, SLOT( newData( const QByteArray & ) ) );
  53. }
  54.  
  55. void View::downloadFile()
  56. {
  57.     // QString file = QFileDialog::getOpenFileName();
  58.     // under Windows you must not use the native file dialog
  59.     QString file = getOpenFileName();
  60.     if ( !file.isEmpty() ) {
  61.     // clear the view
  62.     fileView->clear();
  63.     
  64.     // download the data
  65.     op = file;
  66.     op.get();
  67.     }
  68. }
  69.  
  70. QString View::getOpenFileName()
  71. {
  72.     static QString workingDirectory = QDir::currentDirPath();
  73.  
  74.     QFileDialog *dlg = new QFileDialog( workingDirectory,
  75.         QString::null, 0, 0, TRUE );
  76.     dlg->setCaption( QFileDialog::tr( "Open" ) );
  77.     dlg->setMode( QFileDialog::ExistingFile );
  78.     QString result;
  79.     if ( dlg->exec() == QDialog::Accepted ) {
  80.     result = dlg->selectedFile();
  81.     workingDirectory = dlg->url();
  82.     }
  83.     delete dlg;
  84.     return result;
  85. }
  86.  
  87. void View::newData( const QByteArray &ba )
  88. {
  89.     // append new data
  90.     fileView->append( ba );
  91. }
  92.