/* Copyright 2003 Rikard Björklind, Mikael Gransell This file is part of dc-qt. dc-qt 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. dc-qt 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 dc-qt; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "connect_dlg.h" #include "dc_parse.h" #include "hub_lv_item.h" #include "dc_settings.h" #include "util.h" #include "debugdlg.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; ConnectDlg::ConnectDlg(dc_hub *h, QWidget *p /*= 0*/, const char *name /*= 0*/, bool modal /*= false*/) : QDialog(p, name, modal), hubListTimer(0) { wnd_setting wndset(settings.get_setting("wndset_connect")); if (wndset.isValid()) { move(QPoint(wndset.x(), wndset.y())); resize(wndset.w(), wndset.h()); } setCaption("dc-qt: Connect"); // this object contains the listing of hubs hubs = h; connect(hubs,SIGNAL(got_list()),this,SLOT(update_hublist())); gl_layout = new QVBoxLayout(this,0,2); // // Header // //QLabel *header = new QLabel(this); //header->setText("

Connect

Select a hub to connect to or enter the address directly in the quickconnect field, then click connect.
Note that the profile setting only affects public hubs and quickconnect.
"); //header->setBackgroundMode( PaletteBase ); //header->setFrameShape(QFrame::Box); //header->setFrameShadow(QFrame::Sunken); //header->setLineWidth(2); //gl_layout->addWidget(header); // // Quickconnect & Profile selector // QHBox *box1 = new QHBox(this); QGroupBox *qcgb = new QGroupBox(1,Qt::Vertical,"QuickConnect",box1); quickConnectEdit = new QLineEdit(qcgb,"quickconnectedit"); QGroupBox *pgb = new QGroupBox(1,Qt::Vertical,"Profile",box1); cnxProfile = new QComboBox(pgb); //QStringList profiles = settings.get_profiles(); cnxProfile->insertStringList(settings.get_profiles()); box1->setSpacing(10); tab = new QTabWidget(this,"connection tab"); QVBox *tab1 = new QVBox; QVBox *tab2 = new QVBox; QHBox *tab3 = new QHBox; // Create tab 1. // list view of hubs in the first row // three buttons side by side along the bottom //gl_layout = new QGridLayout(this, 3, 3); // Hublist controls QHBox *hlbox = new QHBox(tab1); QLabel *urlLbl = new QLabel("Hublist &URL: ",hlbox); hubListSelector = new QComboBox(true,hlbox,"hublistselector"); urlLbl->setBuddy(hubListSelector); hubListSelector->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Minimum); hubListSelector->setAutoCompletion(true); hubListSelector->setDuplicatesEnabled(false); hubListSelector->setInsertionPolicy(QComboBox::AtTop); hubListSelector->setMaxCount(10); hubListSelector->insertStringList( settings.get_hub_lists() ); refreshBtn = new QPushButton("Fetch",hlbox); hublistProgress = new QProgressBar(0,hlbox,"hublist progress"); hublistProgress->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Minimum); hublistProgress->setMaximumWidth(200); connect(refreshBtn, SIGNAL(clicked()), SLOT(refresh_listing())); connect(hubListSelector,SIGNAL(activated(int)),this,SLOT(refresh_listing())); //connect(hubListSelector,SIGNAL(textChanged(QString&)),this,SLOT(refresh_listing())); // HubList filter QHBox *searchBox = new QHBox(tab1); QLabel *filterLbl = new QLabel("&Show hubs where ",searchBox); filterCombo = new QComboBox(searchBox); new QLabel(" contains ",searchBox); hubFilterEdit = new QLineEdit(searchBox); filterLbl->setBuddy(hubFilterEdit); filterCombo->insertItem("Hub name"); filterCombo->insertItem("Comment"); connect(hubFilterEdit,SIGNAL(textChanged(const QString& )),this,SLOT(filter())); connect(filterCombo,SIGNAL(activated(int)),this,SLOT(filter())); lv_hubs = new QListView(tab1, "connect_dlg_hubs"); lv_hubs->addColumn("Hub Name"); lv_hubs->addColumn("Hub IP"); lv_hubs->addColumn("Comment"); lv_hubs->addColumn("Users"); lv_hubs->setColumnWidthMode(0, QListView::Manual); lv_hubs->setColumnWidthMode(1, QListView::Manual); lv_hubs->setColumnWidthMode(2, QListView::Manual); lv_hubs->setColumnWidthMode(3, QListView::Manual); lv_hubs->setColumnWidth(0, 300); lv_hubs->setColumnWidth(1, 150); lv_hubs->setColumnWidth(2, 300); lv_hubs->setColumnWidth(3, 50); // load the hub lists hubs->load_list_from_disk(); hubs->load_running_hubs(); // refresh_listing(); // add the public hubs to the listview //public_hubs = hubs->get_hubs(); //add_hubs_to_list(public_lv_items, lv_hubs, public_hubs); menu = new QPopupMenu(lv_hubs,"connection menu"); menu->insertItem("Connect",0); menu->insertItem("Add to favourites",1); connect(menu,SIGNAL(activated(int)),SLOT(menu_clicked(int))); connect(lv_hubs,SIGNAL(rightButtonClicked(QListViewItem*,const QPoint&,int)), SLOT(show_menu(QListViewItem*,const QPoint&,int))); connect(lv_hubs,SIGNAL(doubleClicked(QListViewItem*)),this,SLOT(accept())); tab->addTab(tab1,"&Public"); // Create tab 2: favourites. This is one mess, need to shape up the layout later. /*favList = new QListView(tab2,"favourites list"); favList->addColumn("Name"); favList->addColumn("Address"); favList->addColumn("Autoconnect"); favList->addColumn("Profile"); favList->setAllColumnsShowFocus(true); connect(favList,SIGNAL(doubleClicked(QListViewItem*)), this, SLOT(accept())); */ favList = new QTable( settings.num_favourites(),4,tab2,"favtable"); favList->setSelectionMode(QTable::SingleRow); favList->setLeftMargin(0); QStringList colLabels; colLabels+="Name"; colLabels+="Address"; colLabels+="Autoconnect"; colLabels+="Profile"; favList->setColumnLabels(colLabels); favList->setColumnWidth(0,200); favList->setColumnStretchable(1,true); favList->setShowGrid(false); favList->setFocusStyle(QTable::FollowStyle); connect(favList,SIGNAL(doubleClicked( int , int , int , const QPoint&)),this,SLOT(accept())); connect(favList,SIGNAL(valueChanged(int,int)),SLOT(updateFavourite(int,int))); QHBox *hbox = new QHBox(tab2); QPushButton *newBtn = new QPushButton("New",hbox); QPushButton *editBtn = new QPushButton("Edit",hbox); QPushButton *removeBtn = new QPushButton("Remove",hbox); for(int i=0;i < settings.num_favourites();i++) { QString name,address,p,prof; bool autoc; settings.get_favourite(i,name,address,p,autoc,prof); /*//favList->insertItem(name); new QListViewItem(favList,name,address);*/ favList->setItem(i,0,new QTableItem(favList,QTableItem::Never,name)); favList->setItem(i,1,new QTableItem(favList,QTableItem::Never,address)); QCheckTableItem *cti = new QCheckTableItem(favList,""); cti->setChecked(autoc); favList->setItem(i,2,cti ); QComboTableItem *comb = new QComboTableItem(favList,settings.get_profiles()); comb->setCurrentItem(prof); favList->setItem(i,3,comb); } //if (favList->firstChild()) // favList->setSelected(favList->firstChild(), true); tab->addTab(tab2,"&Favourites"); //tab2->setStretchFactor(favList,20); //tab2->setStretchFactor(vbox,1); //QSpacerItem *spacer = new QSpacerItem(300,10,QSizePolicy::Expanding,QSizePolicy::Expanding); //spacer->setAlignment(Qt::AlignRight); //tab2->layout()->addItem(spacer); connect(newBtn, SIGNAL(clicked()), this, SLOT(addFavourite())); connect(editBtn, SIGNAL(clicked()), this, SLOT(editFavourite())); connect(removeBtn, SIGNAL(clicked()), this, SLOT(removeFavourite())); // Tab 3: Running hubs QVBox *running_vbox = new QVBox(tab3); run_lv = new QListView(tab3, "running list"); run_lv->addColumn("Hub Name"); run_lv->addColumn("Hub IP"); run_lv->addColumn("Comment"); run_lv->addColumn("Users"); run_lv->setAllColumnsShowFocus(true); running_vbox->layout()->addItem(new QSpacerItem(10, 300)); connect(run_lv,SIGNAL(doubleClicked(QListViewItem*)),this,SLOT(accept())); // add the running hubs to the listview running_hubs = hubs->get_running_hubs(); add_hubs_to_list(running_lv_items, run_lv, running_hubs); if (run_lv->firstChild()) run_lv->setSelected(run_lv->firstChild(), true); tab->addTab(tab3, "Running"); tab3->layout()->setSpacing(0); //gl_layout->addMultiCellWidget(tab, 0, 0, 0, 2); gl_layout->addWidget(tab); gl_layout->addWidget(box1); // Add the rest - non-tab stuff QHBox *btnbox = new QHBox(this); btnbox->setMargin(4); btnbox->setSpacing(10); pb_ok = new QPushButton("C&onnect", btnbox); pb_cancel = new QPushButton("&Cancel", btnbox); btnbox->setStretchFactor(pb_ok,2); gl_layout->addWidget(btnbox); //pb_get_hubs = new QPushButton("&Refresh Listing", this); //quickConnectEdit = new QLineEdit(this,"quickconnectedit"); //QLabel *quickLbl = new QLabel("&Quick Connect:",this); //quickLbl->setBuddy(quickConnectEdit); //gl_layout->addWidget( quickLbl, 1,0); //gl_layout->addWidget( quickConnectEdit ,1, 1); //gl_layout->addWidget(pb_ok, 2, 1); //gl_layout->addWidget(pb_cancel, 2, 2); // size dialog setMaximumWidth(1280); connect(quickConnectEdit,SIGNAL(returnPressed()),this,SLOT(accept())); connect(pb_ok, SIGNAL(clicked()), SLOT(accept())); connect(pb_cancel, SIGNAL(clicked()), SLOT(reject())); //connect(pb_get_hubs, SIGNAL(clicked()), SLOT(refresh_listing())); pb_ok->setDefault(false); pb_ok->setAutoDefault(false); pb_cancel->setAutoDefault(false); pb_cancel->setDefault(false); } ConnectDlg::~ConnectDlg() { clear_list(public_lv_items); clear_list(running_lv_items); } void ConnectDlg::closeEvent(QCloseEvent * e) { // save window position and size wnd_setting wndset(x(), y(), width(), height()); if (wndset.isValid()) { settings.set_setting("common","wndset_connect", wndset.toString()); } e->accept(); } void ConnectDlg::filter(/*const QString& str*/) { int col = 0; const QString str = hubFilterEdit->text(); if(filterCombo->currentItem()==1) col = 2; QListViewItemIterator it(lv_hubs); while( it.current() ) { if( str.isEmpty()) it.current()->setVisible(true); else it.current()->setVisible( it.current()->text(col).contains(str,false) ); ++it; } } // given a list of hub_info*, add each hub to the given listview void ConnectDlg::add_hubs_to_list(vector &l, QListView *lv, const vector &h) { for (unsigned int i = 0; i < h.size(); i++) l.push_back(new hub_lv_item(lv, h[i]->name, h[i]->ip, h[i]->comment, h[i]->users)); } // clear the list of hubs, and delete each object, which removes // the items from the listview void ConnectDlg::clear_list(vector &l) { while (l.size() > 0) { hub_lv_item* h = l.back(); l.pop_back(); delete h; } } // return the currently selected hub // if no hub is selected, return an empty hub_info object const hub_info ConnectDlg::selected_hub() { // Check if there's anything in the quick connect field if(!quickConnectEdit->text().isEmpty()) { return hub_info("",quickConnectEdit->text(),"",""); } // we're on the public hub tab if(tab->currentPageIndex() == 0) { if (lv_hubs->selectedItem()==NULL) return hub_info("", "", "", ""); return hubs->get_public_hub(lv_hubs->selectedItem()->text(HUBIP)); } // we're on the favourites hub tab if(tab->currentPageIndex() == 1) { //if(favList->selectedItem() == NULL) //if(favList->currentItem() == NULL) // return hub_info("", "", "", ""); //return settings.get_favourite(favList->selectedItem()->text(0)); int s = favList->currentSelection(); if(s==-1) return hub_info("", "", "", ""); return settings.get_favourite( favList->item(favList->selection(s).topRow(),0)->text()); } // we're on the running hub tab // if(tab->currentPageIndex() == 2) { if (!run_lv->selectedItem()) return hub_info("", "", "", ""); return hubs->get_running_hub(run_lv->selectedItem()->text(HUBIP)); } } const QString& ConnectDlg::selected_profile() { // Use global profile selector if quickconnect was used if(!quickConnectEdit->text().isEmpty()) currentProfile = cnxProfile->currentText(); else if(tab->currentPageIndex()==0) // on public hub tab currentProfile = cnxProfile->currentText(); else if(tab->currentPageIndex()==1) // on favourites tab { int s = favList->currentSelection(); if(s==-1) currentProfile="default"; // this should never happen else currentProfile = ((QComboTableItem*)favList->item(favList->selection(s).topRow(),3))->currentText(); } else currentProfile = "default"; return currentProfile; } // return true iff the running hub tab is selected bool ConnectDlg::selected_running() { return tab->currentPageIndex() == 2; } // reload the hub list from the web void ConnectDlg::refresh_listing() { // If the timer is active, fetching is in progress, abort it and return. //if(hubListTimer) return; if(hubListTimer) { hubs->abortLoad(); refreshBtn->setText("Fetch"); hubListSelector->setEnabled(true); hubListTimer->stop(); delete hubListTimer; hubListTimer = 0; return; } // Disable the button //refreshBtn->setEnabled(false); refreshBtn->setText("Abort"); hubListSelector->setEnabled(false); // clear the current hubs clear_list(public_lv_items); // do the actual getting of the list hublistProgress->setProgress(0); hubListTimer = new QTimer(this); connect(hubListTimer,SIGNAL(timeout()),this,SLOT(updateProgress())); hubListTimer->start(300); if(hubListSelector->currentText().isEmpty()) hubs->load_list_from_web(QString::null); else hubs->load_list_from_web(hubListSelector->currentText()); // Move the selected item to the top of the combobox. if( hubListSelector->currentItem() != 0 ) { QString curtext = hubListSelector->currentText(); hubListSelector->removeItem(hubListSelector->currentItem()); hubListSelector->insertItem(curtext,0); hubListSelector->setCurrentItem(0); } // Save our little list of hublists. QStringList cp; for(int i=0;i < hubListSelector->count();i++) cp+=hubListSelector->text(i); settings.set_hub_lists( cp ); } void ConnectDlg::updateProgress() { hublistProgress->setProgress( hublistProgress->progress()+6 ); } // This function is called when the hublist has arrived. void ConnectDlg::update_hublist() { debugWin->print("Tjohej!"); if(hubListTimer) { debugWin->print("Stopping timer..."); hubListTimer->stop(); delete hubListTimer; hubListTimer = 0; } // add the hubs to the listview vector h = hubs->get_hubs(); add_hubs_to_list(public_lv_items, lv_hubs, h); // Enable the button again //refreshBtn->setEnabled(true); refreshBtn->setText("Fetch"); hubListSelector->setEnabled(true); hublistProgress->setProgress(0); } void ConnectDlg::addFavourite() { bool ok; QString name = QInputDialog::getText("Enter an alias","Enter an alias:",QLineEdit::Normal,QString::null,&ok); if(!ok) return; QString address = QInputDialog::getText("Enter address","Enter a hostname or IP:",QLineEdit::Normal,QString::null,&ok); if(!ok) return; QString password = QInputDialog::getText("Enter password","Enter a password or leave blank for none:",QLineEdit::Password,QString::null,&ok); if(!ok) return; //new QListViewItem(favList,name,address); int i = favList->numRows(); favList->setNumRows(favList->numRows()+1); favList->setItem(i,0,new QTableItem(favList,QTableItem::Never,name)); favList->setItem(i,1,new QTableItem(favList,QTableItem::Never,address)); favList->setItem(i,2,new QCheckTableItem(favList,"")); favList->setItem(i,3,new QComboTableItem(favList,settings.get_profiles())); settings.add_favourite(name,address,password,false,"default"); } void ConnectDlg::editFavourite() { int s = favList->currentSelection(); if(s==-1) return; QString current_name = favList->item(favList->selection(s).topRow(),0)->text(); QString current_ip = favList->item(favList->selection(s).topRow(),1)->text(); QString current_pwd = settings.get_favourite(current_name).pwd; bool autoc = ((QCheckTableItem*)favList->item(favList->selection(s).topRow(),2))->isChecked(); QString profile = ((QComboTableItem*)(favList->item(favList->selection(s).topRow(),3)))->currentText(); bool ok; QString name = QInputDialog::getText("Enter an alias","Enter an alias:",QLineEdit::Normal, current_name,&ok); if(!ok) return; QString address = QInputDialog::getText("Enter address","Enter a hostname or IP:", QLineEdit::Normal,current_ip,&ok); if(!ok) return; QString pwd = QInputDialog::getText("Enter password","Enter password or leave blank for none:", QLineEdit::Password,current_pwd,&ok); if(!ok) return; settings.del_favourite(current_name); settings.add_favourite(name,address,pwd,autoc,profile); favList->setText( favList->selection(s).topRow(),0,name); favList->setText( favList->selection(s).topRow(),1,address); } void ConnectDlg::removeFavourite() { int s = favList->currentSelection(); if(s==-1) return; settings.del_favourite(favList->item(favList->selection(s).topRow(),0)->text()); favList->removeRow(favList->selection(s).topRow()); } void ConnectDlg::menu_clicked(int i) { if(i==0) accept(); else if(i==1) if(last_clicked) { favList->setNumRows( favList->numRows()+1); favList->setItem(favList->numRows()-1,0,new QTableItem(favList,QTableItem::Never,last_clicked->text(0))); favList->setItem(favList->numRows()-1,1,new QTableItem(favList,QTableItem::Never,last_clicked->text(1))); favList->setItem(favList->numRows()-1,2,new QCheckTableItem(favList,"")); favList->setItem(favList->numRows()-1,3,new QComboTableItem(favList,settings.get_profiles())); /*new QListViewItem(favList, last_clicked->text(0), last_clicked->text(1));*/ settings.add_favourite(last_clicked->text(0), last_clicked->text(1), QString::null,false,"default"); } } void ConnectDlg::show_menu(QListViewItem* item,const QPoint &p, int) { menu->popup(p); last_clicked = item; } void ConnectDlg::updateFavourite(int r,int c) { //debugWin->print("UpdateFavorite " + QString::number(r) + "," + QString::number(c)); // Someone changed something, update! QString name,ip,pwd,prof; bool autoc; settings.get_favourite(r,name,ip,pwd,autoc,prof); if(c==2) { autoc = ((QCheckTableItem*)favList->item(r,c))->isChecked(); } else if(c==3) { prof = ((QComboTableItem*)favList->item(r,c))->currentText(); } settings.add_favourite(name,ip,pwd,autoc,prof); }