Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.83 KB | None | 0 0
  1. package texteditor;
  2.  
  3. import com.trolltech.qt.core.QByteArray;
  4. import com.trolltech.qt.core.QIODevice;
  5. import com.trolltech.qt.core.QRegExp;
  6. import com.trolltech.qt.core.QSize;
  7. import com.trolltech.qt.core.Qt.Orientation;
  8. import com.trolltech.qt.gui.QAction;
  9. import com.trolltech.qt.gui.QApplication;
  10. import com.trolltech.qt.gui.QDialog;
  11. import com.trolltech.qt.gui.QHBoxLayout;
  12. import com.trolltech.qt.gui.QIcon;
  13. import com.trolltech.qt.gui.QLabel;
  14. import com.trolltech.qt.gui.QLineEdit;
  15. import com.trolltech.qt.gui.QListWidget;
  16. import com.trolltech.qt.gui.QPushButton;
  17. import com.trolltech.qt.gui.QSlider;
  18. import com.trolltech.qt.gui.QSplitter;
  19. import com.trolltech.qt.gui.QStyle;
  20. import com.trolltech.qt.gui.QStyleOptionTab.TabPosition;
  21. import com.trolltech.qt.gui.QTabWidget;
  22. import com.trolltech.qt.gui.QTextBrowser;
  23. import com.trolltech.qt.gui.QTextEdit;
  24. import com.trolltech.qt.gui.QTextTable;
  25. import com.trolltech.qt.gui.QTextTableFormat;
  26. import com.trolltech.qt.gui.QToolBar;
  27. import com.trolltech.qt.gui.QVBoxLayout;
  28. import com.trolltech.qt.gui.QWidget;
  29. import com.trolltech.qt.network.QTcpSocket;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32.  
  33. /**
  34. *
  35. * @author thomas
  36. */
  37. public class IRC extends QWidget {
  38. private QTcpSocket socket;
  39.  
  40. private String server;
  41. private int port;
  42. private String password;
  43. private String nickname;
  44.  
  45. private IRCSettingsDialog settingsDialog;
  46.  
  47. private QSplitter splSplitter;
  48.  
  49. private Chat serverChat;
  50.  
  51. private QLabel labServer;
  52. private QLabel labPort;
  53.  
  54. private QToolBar tlbToolbar;
  55.  
  56. private QVBoxLayout layMain;
  57. private QVBoxLayout layRight;
  58. private QHBoxLayout layLeft;
  59. private QHBoxLayout layBottom;
  60.  
  61. private QWidget wgtRight;
  62. private QWidget wgtLeft;
  63. private QWidget wgtBottom;
  64. private QTabWidget twgtChats;
  65.  
  66. private QPushButton btnConnect;
  67. private QPushButton btnDisconnect;
  68. private QPushButton btnSend;
  69. private QPushButton btnSettings;
  70.  
  71. private QLineEdit txtInput;
  72. private QLineEdit txtServer;
  73. private QLineEdit txtPort;
  74. private ArrayList<Chat> txtChats = new ArrayList<Chat>();
  75.  
  76. private QAction actConnect;
  77. private QAction actDisconnect;
  78.  
  79. public IRC() {
  80. socket = new QTcpSocket();
  81. twgtChats = new QTabWidget();
  82.  
  83. server = "irc.mozilla.org";
  84. port = 6667;
  85. password = "sdasdkad21";
  86. nickname = "Nipplermoo";
  87.  
  88. tlbToolbar = new QToolBar();
  89. tlbToolbar.setFloatable(false);
  90. tlbToolbar.setMovable(false);
  91. tlbToolbar.setIconSize(new QSize(16, 16));
  92.  
  93. actConnect = new QAction(getIcon("/connect.png"), tr("Connect"), this);
  94. actConnect.triggered.connect(this, "connect()");
  95. actDisconnect = new QAction(getIcon("/disconnect.png"), tr("Disconnect"), this);
  96. actDisconnect.triggered.connect(this, "disconnectServer()");
  97. actDisconnect.setEnabled(false);
  98.  
  99. tlbToolbar.addAction(actConnect);
  100. tlbToolbar.addAction(actDisconnect);
  101.  
  102.  
  103. labServer = new QLabel("Server");
  104. labPort = new QLabel("Port");
  105.  
  106. wgtRight = new QWidget();
  107. wgtLeft = new QWidget();
  108. wgtBottom = new QWidget();
  109.  
  110. txtInput = new QLineEdit();
  111. txtInput.returnPressed.connect(this, "chatSend()");
  112.  
  113. txtServer = new QLineEdit(server);
  114. txtPort = new QLineEdit(port + "");
  115.  
  116. btnSend = new QPushButton("Send");
  117.  
  118. btnSend.clicked.connect(this, "chatSend()");
  119.  
  120. layMain = new QVBoxLayout();
  121. layMain.setMargin(0);
  122. layRight = new QVBoxLayout();
  123. layRight.setMargin(0);
  124. layLeft = new QHBoxLayout();
  125. layLeft.setMargin(0);
  126. layBottom = new QHBoxLayout();
  127. layBottom.setMargin(0);
  128.  
  129. wgtRight.setLayout(layRight);
  130. wgtLeft.setLayout(layLeft);
  131. wgtBottom.setLayout(layBottom);
  132. wgtBottom.setFixedHeight(32);
  133.  
  134.  
  135. layRight.addWidget(new QListWidget());
  136.  
  137. layLeft.addWidget(twgtChats);
  138.  
  139. layBottom.addWidget(txtInput);
  140. layBottom.addWidget(btnSend);
  141.  
  142. splSplitter = new QSplitter(Orientation.Horizontal);
  143. splSplitter.addWidget(wgtLeft);
  144. splSplitter.addWidget(wgtRight);
  145. ArrayList<Integer> sizes = new ArrayList<Integer>();
  146. sizes.add(1000);
  147. sizes.add(200);
  148. splSplitter.setSizes(sizes);
  149.  
  150. layMain.addWidget(tlbToolbar);
  151. layMain.addWidget(splSplitter);
  152. layMain.addWidget(wgtBottom);
  153.  
  154. setLayout(layMain);
  155. show();
  156. resize(768, 320);
  157.  
  158. serverChat = addChat("Server");
  159.  
  160. }
  161.  
  162. private Chat addChat(String title) {
  163. Chat chat = new Chat(title);
  164. twgtChats.addTab(chat, title);
  165. txtChats.add(chat);
  166. twgtChats.setCurrentWidget(chat);
  167. return chat;
  168. }
  169.  
  170. private Chat getActiveChat() {
  171. return (Chat) twgtChats.currentWidget();
  172. }
  173.  
  174. public void connect() {
  175. txtServer.setDisabled(true);
  176. txtPort.setDisabled(true);
  177. labServer.setDisabled(true);
  178. labPort.setDisabled(true);
  179.  
  180. actConnect.setDisabled(true);
  181. actDisconnect.setEnabled(true);
  182.  
  183. getActiveChat().addLine("Connecting to " + server + ":" + port);
  184. server = txtServer.text();
  185. port = Integer.parseInt(txtPort.text());
  186.  
  187.  
  188. socket.connectToHost("irc.quakenet.org", 6667, new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadWrite));
  189. socket.connected.connect(this, "connected()");
  190. socket.disconnected.connect(this, "disconnected()");
  191. socket.readyRead.connect(this, "read()");
  192. socket.error.connect(this, "error()");
  193.  
  194. }
  195.  
  196. private void promtJoin() {
  197.  
  198. }
  199.  
  200. private void disconnectServer() {
  201.  
  202. }
  203.  
  204. private void connected () {
  205. getActiveChat().addLine("CONNECTED TO HOST!");
  206. send("PASS " + password);
  207. send("NICK " + nickname);
  208. send("USER guest 0 * :Mooper32");
  209.  
  210. }
  211.  
  212. private void error() {
  213. getActiveChat().addLine("ERROR! Something fucked up!");
  214. }
  215.  
  216. private void send (String str) {
  217. QByteArray bytes = new QByteArray(str);
  218. socket.write(bytes.append("\r\n"));
  219. getActiveChat().addLine(bytes.toString());
  220. }
  221.  
  222. private void disconnected () {
  223. getActiveChat().addLine("Disconnected :C");
  224. }
  225.  
  226. private void read () {
  227. while (!socket.atEnd()) {
  228. processMessage(socket.readLine().toString());
  229. }
  230. }
  231.  
  232. private void processMessage(String msg) {
  233. serverChat.addLine(msg);
  234.  
  235. if (msg.startsWith("PING :")) {
  236. serverChat.addLine("I GOT A PING!");
  237. send("PONG :" + msg.substring(6));
  238. return;
  239. }
  240.  
  241.  
  242.  
  243. // Run through messages by code
  244. String[] msgs = msg.split(" ");
  245. if (msgs.length <= 1) return;
  246.  
  247. // On channel start / namelist
  248. if (msgs[1].matches("353")) {
  249. if (!chatIsOpen(msgs[4])) {
  250. addChat(msgs[4]);
  251. }
  252. for (int i=5; i<msgs.length; i++) {
  253. getChat(msgs[4]).addUser(msgs[i]);
  254. System.out.println("adding user " + msgs[i]);
  255. }
  256. }
  257.  
  258. // Message Reveiced from some other user
  259. if (msgs[1].matches("PRIVMSG")) {
  260. if (!chatIsOpen(msgs[2])) {
  261. addChat(msgs[2]);
  262. }
  263. }
  264.  
  265.  
  266.  
  267. }
  268.  
  269. private Chat getChat(String name) {
  270. for (Chat chat : txtChats) {
  271. if (name.matches(chat.getName())) {
  272. return chat;
  273. }
  274. }
  275. return null;
  276. }
  277.  
  278. private boolean chatIsOpen(String name) {
  279. for (Chat chat : txtChats) {
  280. if (name.matches(chat.getName())) {
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286.  
  287. private void chatSend() {
  288. if (txtInput.text().length() == 0) return;
  289. if (txtInput.text().startsWith("/")) {
  290. send(txtInput.text().substring(1));
  291. } else {
  292. send("PRIVMSG " + getActiveChat().getName() + " " + txtInput.text());
  293. }
  294. txtInput.clear();
  295. }
  296.  
  297. public static void main(String[] args) {
  298. QApplication.initialize(args);
  299. IRC irc = new IRC();
  300. QApplication.exec();
  301. }
  302.  
  303.  
  304. private void showSettings() {
  305. if (settingsDialog == null) {
  306. settingsDialog = new IRCSettingsDialog(this);
  307. }
  308. settingsDialog.show();
  309. }
  310.  
  311. private QIcon getIcon(String filename) {
  312. return new QIcon("icons/" + filename);
  313. }
  314.  
  315. }
  316.  
  317. class IRCSettingsDialog extends QDialog {
  318. IRC parent;
  319. public IRCSettingsDialog(IRC irc) {
  320. parent = irc;
  321. }
  322. }
  323.  
  324. class Chat extends QTextEdit {
  325. private String name;
  326. private ArrayList<String> users = new ArrayList<String>();
  327.  
  328. public Chat (String name) {
  329. this.name = name;
  330. show();
  331. setFontFamily("Courier New");
  332. document().contentsChanged.connect(this, "scrollDown()");
  333. }
  334.  
  335. private void scrollDown() {
  336. scroll(0, 0);
  337. }
  338.  
  339. public String getName() {
  340. return name;
  341. }
  342.  
  343. public void addLine(String line) {
  344. append(line.replaceAll("\n", "").replaceAll("\r", ""));
  345. }
  346.  
  347. public void addUser(String name) {
  348. users.add(name);
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement