GuiActiveUserList.cpp

IdĽ do dokumentacji tego pliku.
00001 #include <QTreeWidget>
00002 #include <QMessageBox>
00003 #include <QMenu>
00004 #include <QString>
00005 #include <QHostAddress>
00006 
00007 #include "exeptions.h"
00008 #include "GuiActiveUserList.h"
00009 
00010 #include <QHeaderView>
00011 
00012 
00013 
00014 GuiActiveUserList::GuiActiveUserList(QWidget *parent): QTreeWidget (parent)
00015 {
00016         this->my_nick.clear();
00017 
00018         QHeaderView *head = new QHeaderView(Qt::Horizontal,this);
00019         head->setMovable ( 0);
00020         head->setResizeMode ( QHeaderView::Stretch);
00021 
00022         this->setHeader ( head);
00023         this->setHeaderLabels ( QStringList (tr("Aktywni Użytkownicy")));
00024         this->setContextMenuPolicy (Qt::CustomContextMenu);
00025 
00026         //SelectionBehavior(QAbstractItemView::SelectRows)
00027         this->setSelectionMode (QAbstractItemView::SingleSelection);
00028         //      this->setSelectionBehavior (QAbstractItemView::SelectItems);
00029 
00030 
00031         //podwójne kliknięcie rozmowa prywatna
00032         connect (this, SIGNAL(itemDoubleClicked ( QTreeWidgetItem * , int  )), this, SLOT(sendSignalPrivateChat()));
00033 
00034         //kliknięcie prawym klawiszem myszy otwiera menu z opcjami..
00035         connect (this, SIGNAL( customContextMenuRequested(const QPoint&)), this, SLOT(showUserMenu (const QPoint&)));
00036 
00037 
00038 }
00039 
00040 void GuiActiveUserList::demo()
00041 {
00042 
00043         QString tekst("pebek");
00044         QHostAddress ip;
00045         ip.setAddress ("10.10.10.1");
00046         this->addUser(ip, tekst);
00047 
00048         QString tekst2("n0rn");
00049         QHostAddress ip2;
00050         ip2.setAddress ("10.10.10.2");
00051         this->addUser(ip2, tekst2);
00052 
00053         QString tekst3("gjmp");
00054         QHostAddress ip3;
00055         ip3.setAddress ("10.10.10.3");
00056         this->addUser(ip3, tekst3);
00057 
00058         QString tekst4("pkw");
00059         QHostAddress ip4;
00060         ip4.setAddress ("10.10.10.4");
00061         this->addUser(ip4, tekst4);
00062 
00063         QString tekst5("luik");
00064         QHostAddress ip5;
00065         ip5.setAddress ("10.10.10.5");
00066         this->addUser(ip5, tekst5);
00067 
00068 
00069 }
00070 
00071 QString GuiActiveUserList::returnNameUser(QHostAddress host)
00072 {
00073         QString nick(this->all_users.key(host));
00074         return nick;
00075 }
00076 
00077 QHostAddress GuiActiveUserList::returnIpUser(QString nick)
00078 {
00079         QHostAddress ip(this->all_users.value(nick));
00080         return ip;
00081 }
00082 
00083 void GuiActiveUserList::clearAllUsers()
00084 {
00085         //kasujemy zawartości z piękneogo obiektu QListWidget
00086         this -> clear();
00087         //kasujemy zawartości z tablcy hash z nickami i hostami
00088         this->all_users.clear ();
00089 }
00090 
00091 void GuiActiveUserList::setMyNick(QString nick)
00092 {
00093         if(isUserOnList( nick))
00094         {
00095 
00096                 //user istneije taki
00097                                 QString error_nick(tr("Nazwa <b>"));
00098                                 error_nick.append (nick);
00099                                 error_nick.append (tr("</b> jest już używana przez kogoś innego.<br><br>"));
00100                                 error_nick.append (tr("<b> zmień nazwę Użytkownika !! </b>)"));
00101 
00102                 QWidget widget;
00103                 QMessageBox::about(&widget,QString(tr("Wyjątek:")),error_nick);
00104 
00105                 emit disconectChatUserExists();
00106         }
00107         else
00108         {
00109                 //jeśli nie ma takiego usera
00110                 my_nick = nick;
00111                 QHostAddress host;
00112                 host.setAddress ("0.0.0.0");
00113                 //this->addUser(ip, tekst);
00114 
00115                 //jeśli wszystko przebiegło oki, to dodajemy usera do listy
00116                 QTreeWidgetItem * item = new QTreeWidgetItem (this, 0);
00117                 QFont bold;
00118                 bold.setBold ( 1 );
00119                 bold.setUnderline ( 1);
00120                 item->setFont ( 0, bold);
00121                 item->setText ( 0, nick );
00122                 // dodajemy usera do hasa 
00123                 this->all_users.insert (nick, host);
00124                 // i sortujemy liste alfabetycznie 
00125                 this->sortItems ( 0,Qt::AscendingOrder );
00126         }
00127 }
00128 
00129 bool GuiActiveUserList::isUserOnList(QString nick)
00130 {
00131         QTreeWidgetItem * item = new QTreeWidgetItem ();
00132 
00133         // ile lista ma elemetow
00134         int max_row = this->topLevelItemCount ();
00135         int licznik = 0;
00136         // usuwam z listy
00137         while( licznik < max_row)
00138         {
00139                 if( this->topLevelItem ( licznik )->text(0) == nick)
00140                 {
00141                         return 1;
00142                         licznik = max_row;
00143                 }
00144                 licznik++;
00145         }
00146         return 0;
00147 }
00148 
00149 
00150 void GuiActiveUserList::addUser(QHostAddress host, QString nick)
00151 {
00152 
00153         if(this->isUserOnList(nick))
00154         {
00155         //jeśli użytkownik istneije nic nie robimy
00156         }
00157         else
00158         {
00159                 try
00160                 {
00161                         QString find_nick("");
00162                         find_nick = this->returnNameUser(host);
00163 
00164                         QHostAddress find_ip("");
00165                         find_ip = returnIpUser(find_nick);
00166 
00167                         QString test("test");
00168                         QHostAddress ip("");
00169                         ip = this->all_users.value(nick);
00170 
00171                         //jesli ip i nick do dodania jest juz na liscie kontaktow
00172                         if(find_nick == nick)
00173                         {
00174                                         QString error_nick_and_ip(tr("Urzytkownik "));
00175                                         error_nick_and_ip.append (nick);
00176                                         error_nick_and_ip.append (tr(" juz jest zamieszczony na liscie dostepnych, a jego ip to: "));
00177                                         error_nick_and_ip.append (find_ip.toString());
00178 
00179                                 //Jakas lipa, ktora nie dziala
00180                                 throw Exeptions(800,error_nick_and_ip,
00181                                                                         tr(" GuiActiveUserList::addUser(QHostAddress host, QString nick)"),
00182                                                                         tr("w celu ominięcia tego blędu proszę nie wysyalać już dopisanych userów ;D, jeśli nie jesteś developerem proszę poinformuj o tym blędzie autorów programu"));
00183                         }
00184                         else 
00185                         //jeśli  nick do dodania jest już na liście kontaktów (ip może byż inne)
00186                         if(!(ip.isNull ()))
00187                         {
00188                                 /*
00189                                 QString error_nick("Użytkownik ");
00190                                 error_nick.append (nick);
00191                                 error_nick.append (" juz jest zamieszczony na liscie dostepnych, a jego ip to: ");
00192                                 error_nick.append (ip.toString());
00193                                 error_nick.append (" a chial go sobie przypisac wlasciel ip: ");
00194                                 error_nick.append (host.toString());
00195                                 error_nick.append ("<b> zmien nazwe Urzytkownika !! </b>");
00196 
00197 
00198                                 //Jakas lipa, ktora nie dziala
00199                                 throw Exeptions(801,error_nick,
00200                                                                 tr(" GuiActiveUserList::addUser(QHostAddress host, QString nick)"),
00201                                                                 tr("w celu ominiecia tego bledu prosze nie wysyalac juz dopisanych userow ;D, jesli nie jestes developerem prosze pointoruj o tym bledzie autorow programu"));
00202                                 */
00203                         }
00204                         else 
00205                         //jesli adres ip ktos z listy juz ma (moga byc rozne nicki)
00206                         if(find_ip == host)
00207                         {
00208                 
00209                                 QString error_ip(tr("ip "));
00210                                 error_ip.append (host.toString());
00211                                 error_ip.append (tr(" już jest zamieszczony na liście dostępnych, konkretnie ma go: "));
00212                                 error_ip.append (this->returnNameUser(host));
00213                                 error_ip.append (tr(" a chciał go sobie przypisać użytkownik: "));
00214                                 error_ip.append (nick);
00215 
00216                                 //Jakas lipa, ktora nie dziala
00217                                 throw Exeptions(802,error_ip,
00218                                                                 tr(" GuiActiveUserList::addUser(QHostAddress host, QString nick)"),
00219                                                                 tr("w celu ominięcia tego blędu proszę nie wysyałać już dopisanych userów ;D, jeśli nie jesteś developerem proszę poinformuj o tym blędzie autorów programu"));
00220                 
00221                         }
00222 
00223 
00224                         //jeśli wszystko przebiegło oki, to dodajemy usera do listy
00225                         QTreeWidgetItem * item = new QTreeWidgetItem (this, 0);
00226                         item->setText ( 0, nick );
00227                         // dodajemy usera do hasa 
00228                         this->all_users.insert (nick, host);
00229                         // i sortujemy liste alfabetycznie 
00230                         this->sortItems ( 0,Qt::AscendingOrder );
00231 
00232                 }
00233 
00234                 catch(Exeptions & wyjatek)
00235                 {
00236                         QWidget widget;
00237                         QMessageBox::about(&widget,QString(tr("Wyjątek:")),wyjatek.getErrorMessage());
00238                 }
00239         }
00240 }
00241 
00242 void GuiActiveUserList::delUser(QHostAddress host)
00243 {
00244         QTreeWidgetItem * item = new QTreeWidgetItem ();
00245         // poszukiwany wyraz
00246         QString find("");
00247         find = this->returnNameUser(host);
00248         
00249         // ile lista ma elemetow
00250         int max_row = this->topLevelItemCount ();
00251         int licznik = 0;
00252 
00253         // usuwam z listy
00254         while( licznik < max_row)
00255         {
00256                 if( this->topLevelItem ( licznik )->text(0) == find)
00257                 {
00258                         //item = this->takeItem ();
00259                         this->takeTopLevelItem ( licznik);
00260                         licznik = max_row;
00261                 }
00262                 licznik++;
00263         }
00264 
00265         // usuwam z tablicy haszującej
00266         all_users.remove(all_users.key(host));
00267 }
00268 
00269 
00270 void GuiActiveUserList::showUserMenu (const QPoint &cos)
00271 {
00272 
00273         QTreeWidgetItem *item = NULL;
00274         item = this->currentItem ();
00275 
00276         if(item != NULL)
00277         {
00278                 QTreeWidgetItem * item = currentItem ();
00279                 QString biezacy_nick;
00280                 biezacy_nick = item->text(0);
00281         
00282                 if( biezacy_nick != my_nick)
00283                 {
00284                         this->user_menu = new QMenu;
00285                         this->user_menu->addAction (tr("rozmowa prywatna"),this, SLOT(sendSignalPrivateChat()));
00286                         this->user_menu->addAction (tr("wyślij plik"), this, SLOT(sendSignalSendFile()));
00287                         this->user_menu->addAction (tr("sprawdź IP"), this, SLOT(sendSignalCheckIp()));
00288                         this->user_menu->addAction (tr("ping"), this, SLOT(sendSignalCheckPing()));
00289 
00290                         this->user_menu->exec (QCursor::pos());
00291                 }
00292                 else
00293                 {
00294 
00295                 }
00296         }
00297 
00298 
00299 }
00300 
00301 
00302 
00303 void GuiActiveUserList::sendSignalPrivateChat()
00304 {
00305         
00306         QTreeWidgetItem * item = currentItem ();
00307 
00308         QString nick;
00309         nick = item->text(0);
00310 
00311         QHostAddress ip("");
00312         ip = this->all_users.value(nick);
00313         if (nick != this->my_nick)
00314         {
00315                 emit signalPrivateChat(ip);
00316         }
00317 }
00318 
00319 void GuiActiveUserList::sendSignalSendFile()
00320 {
00321         QTreeWidgetItem * item = currentItem ();
00322 
00323         QString nick;
00324         nick = item->text(0);
00325 
00326         QHostAddress ip("");
00327         ip = this->all_users.value(nick);
00328         emit signalSendFile(ip);
00329 }
00330 
00331 void GuiActiveUserList::sendSignalCheckIp()
00332 {
00333         QTreeWidgetItem * item = currentItem ();
00334 
00335         QString nick;
00336         nick = item->text(0);
00337 
00338         QHostAddress ip("");
00339         ip = this->all_users.value(nick);
00340 
00341         emit signalCheckIp(ip);
00342 }
00343 
00344 void GuiActiveUserList::sendSignalCheckPing()
00345 {
00346         QTreeWidgetItem * item = currentItem ();
00347 
00348         QString nick;
00349         nick = item->text(0);
00350 
00351         QHostAddress ip("");
00352         ip = this->all_users.value(nick);
00353         emit signalCheckPing(ip);
00354 }
00355 

Wygenerowano Sun Jun 11 12:55:08 2006 dla lanChat programem  doxygen 1.4.6