| 1 | /* |
| 2 | * File: DevicesPane.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "DevicesPane.h" |
| 26 | #include "DevicesRename.h" |
| 27 | #include "MainWindow.h" |
| 28 | #include "VisualizedDeviceEntry.h" |
| 29 | #include "Environment.h" |
| 30 | #include "HerculesStudio.h" |
| 31 | #include "GenericDeviceProperties.h" |
| 32 | #include "StringTokenizer.h" |
| 33 | |
| 34 | #include <QBitmap> |
| 35 | #include <QString> |
| 36 | #include <iostream> |
| 37 | #include <string> |
| 38 | #include <sstream> |
| 39 | #include <cstdio> |
| 40 | |
| 41 | DevicesPane::DevicesPane(QWidget *parent) |
| 42 | : DeviceMenuProcessor(parent), |
| 43 | StatusUpdateCollector(), |
| 44 | mLayout(NULL), |
| 45 | mListView(NULL), |
| 46 | mModel(NULL), |
| 47 | mRenameDlg(NULL), |
| 48 | mOldDevNum(-1) |
| 49 | { |
| 50 | ui.setupUi(this); |
| 51 | std::string iconsPath = Environment::getIconsPath() + "/yellow.png"; |
| 52 | mYellowIcon = new QIcon(iconsPath.c_str()); |
| 53 | iconsPath = Environment::getIconsPath() + "/yellowhigh.png"; |
| 54 | mYellowHighIcon = new QIcon(iconsPath.c_str()); |
| 55 | mMainWindow = dynamic_cast<MainWindow *>(parent); |
| 56 | } |
| 57 | |
| 58 | DevicesPane::~DevicesPane() |
| 59 | { |
| 60 | delete mYellowIcon; |
| 61 | delete mYellowHighIcon; |
| 62 | delete mListView; |
| 63 | mDevices.clear(); |
| 64 | } |
| 65 | |
| 66 | bool DevicesPane::notify(const QByteArray& statusLine) |
| 67 | { |
| 68 | hOutDebug(1,"devices notify:" << statusLine.data() << "."); |
| 69 | const struct DynDeviceLine * line = reinterpret_cast<const DynDeviceLine *>(statusLine.data()); |
| 70 | int devNo; |
| 71 | VisualizedDeviceEntry deviceEntry; |
| 72 | bool deviceAdded = false; |
| 73 | if (strncmp(line->DEV,"DEV",3) == 0) |
| 74 | { |
| 75 | DeviceTypes::Type devType = DeviceTypes::Other; |
| 76 | switch (line->action) |
| 77 | { |
| 78 | case('A'): |
| 79 | if (strncmp(line->devType,"DASD",4)==0) |
| 80 | devType= DeviceTypes::DASD; |
| 81 | else if (strncmp(line->devType,"CTC",3)==0) |
| 82 | devType= DeviceTypes::CTC; |
| 83 | else if (strncmp(line->devType,"OSA",3)==0) |
| 84 | devType= DeviceTypes::Comm; |
| 85 | else if (strncmp(line->devType,"QETH",4)==0) |
| 86 | devType= DeviceTypes::Comm; |
| 87 | else if (strncmp(line->devType,"RDR ",4)==0) |
| 88 | devType= DeviceTypes::CardReader; |
| 89 | else if (strncmp(line->devType,"PCH ",4)==0) |
| 90 | devType= DeviceTypes::CardReader; |
| 91 | else if (strncmp(line->devType,"PRT ",4)==0) |
| 92 | devType= DeviceTypes::Printer; |
| 93 | else if (strncmp(line->devType,"DSP ",4)==0) |
| 94 | devType= DeviceTypes::Terminal; |
| 95 | else if (strncmp(line->devType,"TAPE",4)==0) |
| 96 | devType= DeviceTypes::Tape; |
| 97 | else if (strncmp(line->devType,"CON",3)==0) |
| 98 | devType= DeviceTypes::Console; |
| 99 | |
| 100 | devNo = strtol(line->devNo, NULL, 16); |
| 101 | { |
| 102 | VisualizedDeviceEntry deviceEntryPtr(devNo, devType, statusLine.data()); |
| 103 | std::pair<int,VisualizedDeviceEntry> toinsert(devNo, deviceEntryPtr); |
| 104 | mDevices.insert(toinsert); |
| 105 | hOutDebug(5, "to be added:" << statusLine.data() << " " << (devNo !=0 ? "added" : "NOT ADDED")) ; |
| 106 | if (devNo !=0) deviceAdded = true; // after disconnect and attach, herculess starts sending DEVA instead of DEVC |
| 107 | } |
| 108 | |
| 109 | break; |
| 110 | case('C'): |
| 111 | devNo = strtol(line->devNo, NULL, 16); |
| 112 | if (mDevices.find(devNo) != mDevices.end()) |
| 113 | { |
| 114 | deviceEntry = mDevices[devNo]; |
| 115 | |
| 116 | if (deviceEntry.getItem() != NULL) |
| 117 | { |
| 118 | if (line->statusBytes[1] == '1') |
| 119 | { |
| 120 | deviceEntry.getItem()->setIcon(*mYellowHighIcon); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | deviceEntry.getItem()->setIcon(*mYellowIcon); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | hOutDebug(1,"sending restart"); |
| 131 | mDevices.clear(); |
| 132 | //emit restartDevices(); |
| 133 | } |
| 134 | break; |
| 135 | case('D'): |
| 136 | { |
| 137 | devNo = strtol(line->devNo, NULL, 16); |
| 138 | std::map <int,VisualizedDeviceEntry>::iterator it = mDevices.find(devNo); |
| 139 | if (it != mDevices.end()) |
| 140 | { |
| 141 | mDevices.erase(it); |
| 142 | deviceAdded = true; |
| 143 | } |
| 144 | } |
| 145 | break; |
| 146 | default: |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if (deviceAdded) |
| 152 | { |
| 153 | hOutDebug(3,mDevices.size() << " Devices."); |
| 154 | if (mModel == NULL) mModel = new QStandardItemModel(); |
| 155 | mModel->clear(); |
| 156 | QStandardItem *parentItem = mModel->invisibleRootItem(); |
| 157 | int currLine = 0; |
| 158 | |
| 159 | std::map<int, VisualizedDeviceEntry>::iterator it; |
| 160 | for (int curDev=0; curDev<=DeviceTypes::Comm; curDev++ ) |
| 161 | { |
| 162 | hOutDebug(5,"curDev:" << curDev); |
| 163 | bool titleDevAdded = false; |
| 164 | for (it = mDevices.begin(); it != mDevices.end(); it++) |
| 165 | { |
| 166 | VisualizedDeviceEntry& ent = it->second; |
| 167 | if (ent.getType() == curDev) |
| 168 | { |
| 169 | hOutDebug(5,"ent:" << ent.getDeviceNumber()<<" "<<DeviceTypes::instance().getName(ent.getType())); |
| 170 | if (!titleDevAdded) |
| 171 | { |
| 172 | std::string iconPath = Environment::getIconsPath() + |
| 173 | DeviceTypes::instance().getName(ent.getType()); |
| 174 | const QIcon& ic = DeviceTypes::instance().getIcon(ent.getType()); |
| 175 | QStandardItem *item = new QStandardItem(ic, DeviceTypes::instance().getName(ent.getType())); |
| 176 | item->setEditable(false); |
| 177 | parentItem->appendRow(item); |
| 178 | currLine++; |
| 179 | titleDevAdded=true; |
| 180 | } |
| 181 | char ss[255]; |
| 182 | std::string def = ent.getDefinition(); |
| 183 | size_t pos=def.find('\r'); |
| 184 | while (pos != std::string::npos) |
| 185 | { |
| 186 | def.replace(pos,def.size()," "); |
| 187 | pos = def.find('\r'); |
| 188 | } |
| 189 | sprintf(ss,"%4.4X %s",ent.getDeviceNumber(), def.substr(25).c_str()); |
| 190 | if (ent.getIcon() == NULL) |
| 191 | { |
| 192 | ent.setIcon(mYellowIcon); |
| 193 | } |
| 194 | ent.setItem(new QStandardItem(*ent.getIcon(), ss)); |
| 195 | ent.getItem()->setEditable(false); |
| 196 | std::string tt = ent.getDefinition().substr(10,10).c_str() ; |
| 197 | tt += ent.getDefinition().substr(25).c_str(); |
| 198 | ent.getItem()->setToolTip(tt.c_str()); |
| 199 | ent.setLineNumber(currLine++); |
| 200 | parentItem->appendRow(ent.getItem()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | if (mListView != NULL) |
| 205 | { |
| 206 | mListView->close(); |
| 207 | delete mListView; |
| 208 | mListView = NULL; |
| 209 | } |
| 210 | mListView = new DeviceListView(this); |
| 211 | mListView->setModel(mModel); |
| 212 | mListView->setVisible(true); |
| 213 | adjustSize(); |
| 214 | connect(mListView, SIGNAL(pressed(QModelIndex)),this,SLOT(mousePressed(QModelIndex))); |
| 215 | connect(mListView, SIGNAL(deviceClick(QMouseEvent *)), this, SLOT(devicesClick(QMouseEvent *))); |
| 216 | return true; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | return false; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void DevicesPane::mousePressed(const QModelIndex index) |
| 225 | { |
| 226 | hOutDebug(1,"pressed " << index.row()); |
| 227 | mClickRow = index.row(); |
| 228 | } |
| 229 | |
| 230 | void DevicesPane::adjustSize() |
| 231 | { |
| 232 | if (mListView != NULL) |
| 233 | mListView->setGeometry(0,0,this->size().width()-10,this->size().height()-10); |
| 234 | } |
| 235 | |
| 236 | void DevicesPane::resizeEvent ( QResizeEvent * ) |
| 237 | { |
| 238 | adjustSize(); |
| 239 | } |
| 240 | |
| 241 | QSize DevicesPane::sizeHint() const |
| 242 | { |
| 243 | return QSize(160, 100); |
| 244 | } |
| 245 | |
| 246 | void DevicesPane::clear() |
| 247 | { |
| 248 | hOutDebug(1,"DevicesPane clear"); |
| 249 | mDevices.clear(); |
| 250 | if (mModel != NULL) |
| 251 | mModel->clear(); |
| 252 | if (mListView == NULL) |
| 253 | return; |
| 254 | adjustSize(); |
| 255 | } |
| 256 | |
| 257 | bool DevicesPane::isRealDev(int lineNumber) |
| 258 | { |
| 259 | std::map<int, VisualizedDeviceEntry>::iterator it; |
| 260 | for (it = mDevices.begin(); it != mDevices.end(); it++) |
| 261 | { |
| 262 | VisualizedDeviceEntry& ent = it->second; |
| 263 | hOutDebug(5,"compare: " << ent.getLineNumber() << " with " << lineNumber); |
| 264 | if (ent.getLineNumber() == lineNumber) return true; |
| 265 | } |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | DeviceTypes::Type DevicesPane::getType(int lineNumber) |
| 270 | { |
| 271 | std::map<int, VisualizedDeviceEntry>::iterator it; |
| 272 | for (it = mDevices.begin(); it != mDevices.end(); it++) |
| 273 | { |
| 274 | VisualizedDeviceEntry& ent = it->second; |
| 275 | hOutDebug(5,"compare: " << ent.getLineNumber() << " with " << lineNumber); |
| 276 | if (ent.getLineNumber() == lineNumber) return ent.getType(); |
| 277 | } |
| 278 | return DeviceTypes::Other; |
| 279 | } |
| 280 | |
| 281 | bool DevicesPane::canAddSYSG() |
| 282 | { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | bool DevicesPane::realDevice() |
| 287 | { |
| 288 | return (isRealDev(mClickRow)); |
| 289 | } |
| 290 | |
| 291 | void DevicesPane::doAddDevice(bool) |
| 292 | { |
| 293 | hOutDebug(2,mCandidateLine->getLine()); |
| 294 | std::string command = mCandidateLine->getLine(); |
| 295 | if (command.length() == 0) return; |
| 296 | command = "attach " + command ; |
| 297 | |
| 298 | mMainWindow->issueCommand(command); |
| 299 | mClickRow = -1; |
| 300 | } |
| 301 | |
| 302 | void DevicesPane::menuRename() |
| 303 | { |
| 304 | std::map<int, VisualizedDeviceEntry>::iterator it = mDevices.begin(); |
| 305 | while(it != mDevices.end()) |
| 306 | { |
| 307 | hOutDebug(2, "rename " << mLastClick << " " << it->second.getDeviceNumber() |
| 308 | << " " << it->second.getLineNumber()); |
| 309 | if (it->second.getLineNumber() == mLastClick) |
| 310 | { |
| 311 | int devno = it->second.getDeviceNumber(); |
| 312 | mRenameDlg = new DevicesRename(devno, getNextDev(it), this); |
| 313 | connect (mRenameDlg,SIGNAL(accepted(QString, QString)),this, SLOT(doRename(QString, QString))); |
| 314 | connect (mRenameDlg,SIGNAL(rejected()), this, SLOT(rejected())); |
| 315 | mRenameDlg->exec(); |
| 316 | break; |
| 317 | } |
| 318 | it++; |
| 319 | } |
| 320 | mClickRow=-1; |
| 321 | } |
| 322 | |
| 323 | void DevicesPane::doRename(QString oldDevNum, QString newDevNum) |
| 324 | { |
| 325 | hOutDebug(2, "rename " << oldDevNum.toStdString() << " " << newDevNum.toStdString()); |
| 326 | mOldDevNum = ConfigurationEditor::parseNum(oldDevNum.toStdString(), 16); |
| 327 | std::stringstream command; |
| 328 | command << "define " << oldDevNum.toStdString() << " " << newDevNum.toStdString() ; |
| 329 | mMainWindow->issueCommand(command.str()); |
| 330 | disconnect(mRenameDlg,0,0,0); |
| 331 | mRenameDlg->deleteLater(); |
| 332 | } |
| 333 | |
| 334 | int DevicesPane::getNextDev(std::map<int, VisualizedDeviceEntry>::iterator it) const |
| 335 | { |
| 336 | int ret = it->second.getDeviceNumber()+1; |
| 337 | ++it; |
| 338 | while (it != mDevices.end()) |
| 339 | { |
| 340 | hOutDebug(5,"examining ret=" << ret << " to:" << it->second.getDeviceNumber()); |
| 341 | if (it->second.getDeviceNumber() != ret) |
| 342 | return ret; |
| 343 | else |
| 344 | ret = it->second.getDeviceNumber()+1; |
| 345 | ++it; |
| 346 | } |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | void DevicesPane::rejected() |
| 351 | { |
| 352 | hOutDebug(3, "rejected"); |
| 353 | disconnect(mRenameDlg,0,0,0); |
| 354 | mRenameDlg->deleteLater(); |
| 355 | } |
| 356 | |
| 357 | void DevicesPane::menuDelete() |
| 358 | { |
| 359 | VisualizedDeviceEntry * entry = getDeviceEntry(); |
| 360 | if (entry != NULL) |
| 361 | { |
| 362 | std::stringstream command; |
| 363 | command << "detach " << std::hex << entry->getDeviceNumber() ; |
| 364 | mMainWindow->issueCommand(command.str()); |
| 365 | } |
| 366 | mClickRow=-1; |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | void DevicesPane::menuInterrupt() |
| 371 | { |
| 372 | VisualizedDeviceEntry * entry = getDeviceEntry(); |
| 373 | if (entry != NULL) |
| 374 | { |
| 375 | std::stringstream command; |
| 376 | command << "i " << std::hex << entry->getDeviceNumber() ; |
| 377 | mMainWindow->issueCommand(command.str()); |
| 378 | } |
| 379 | mClickRow=-1; |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | void DevicesPane::menuStatus() |
| 384 | { |
| 385 | VisualizedDeviceEntry * entry = getDeviceEntry(); |
| 386 | if (entry != NULL) |
| 387 | { |
| 388 | std::stringstream command; |
| 389 | command << "ds " << std::hex << entry->getDeviceNumber() ; |
| 390 | mMainWindow->issueCommand(command.str()); |
| 391 | } |
| 392 | mClickRow=-1; |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | bool DevicesPane::traced() |
| 397 | { |
| 398 | VisualizedDeviceEntry * entry = getDeviceEntry(); |
| 399 | if (entry != NULL) |
| 400 | { |
| 401 | return entry->traced(); |
| 402 | } |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | void DevicesPane::menuTraceCCW() |
| 408 | { |
| 409 | VisualizedDeviceEntry * entry = getDeviceEntry(); |
| 410 | if (entry != NULL) |
| 411 | { |
| 412 | std::stringstream command; |
| 413 | command << "t" << (entry->traced() ? "- " : "+ ") << std::hex << entry->getDeviceNumber() ; |
| 414 | mMainWindow->issueCommand(command.str()); |
| 415 | entry->setTraced(!entry->traced()); |
| 416 | } |
| 417 | mClickRow=-1; |
| 418 | return; |
| 419 | } |
| 420 | |
| 421 | bool DevicesPane::hasConfig() |
| 422 | { |
| 423 | VisualizedDeviceEntry * entry = getDeviceEntry(); |
| 424 | if (entry == NULL) return false; |
| 425 | QString devNo = textFromValue(entry->getDeviceNumber()); |
| 426 | mCandidateLine = mMainWindow->getConfigurationFile()->getDevice(devNo.toStdString()); |
| 427 | return mCandidateLine!= NULL; |
| 428 | } |
| 429 | |
| 430 | void DevicesPane::menuProperties() |
| 431 | { |
| 432 | hOutDebug(3,"line=" << mCandidateLine->getLine()); |
| 433 | |
| 434 | GenericDeviceProperties * mProp = NULL; |
| 435 | |
| 436 | try |
| 437 | { |
| 438 | mProp = GenericDeviceProperties::classFactory(*mCandidateLine, |
| 439 | this, |
| 440 | mCandidateLine->getDeviceType()); |
| 441 | } |
| 442 | catch (...) |
| 443 | { |
| 444 | hOutDebug(2, "exception caught in class factory (add)"); |
| 445 | } |
| 446 | |
| 447 | connect(mProp, SIGNAL(updateLine(bool)), this, SLOT(updateDevice(bool))); |
| 448 | mProp->setVisible(true); |
| 449 | mClickRow=-1; |
| 450 | } |
| 451 | |
| 452 | void DevicesPane::updateDevice(bool done) |
| 453 | { |
| 454 | if (!done || mCandidateLine == NULL) |
| 455 | { |
| 456 | mClickRow = -1; |
| 457 | return; |
| 458 | } |
| 459 | hOutDebug(5,mCandidateLine->getLine()); |
| 460 | std::string command = mCandidateLine->getLine(); |
| 461 | command = mCandidateLine->getToken(0) + " " + mCandidateLine->getMultiToken(2, 0); |
| 462 | if (command.length() == 0) return; |
| 463 | command = "devinit " + command ; |
| 464 | |
| 465 | mMainWindow->issueCommand(command); |
| 466 | mClickRow = -1; |
| 467 | } |
| 468 | |
| 469 | void DevicesPane::doLoadTape(QString& tapeFileName) |
| 470 | { |
| 471 | VisualizedDeviceEntry * entry = getDeviceEntry(); |
| 472 | if (entry != NULL) |
| 473 | { |
| 474 | std::stringstream command; |
| 475 | command << "devinit " << std::hex << entry->getDeviceNumber() << " " <<tapeFileName.toStdString() ; |
| 476 | |
| 477 | mMainWindow->issueCommand(command.str()); |
| 478 | } |
| 479 | mClickRow=-1; |
| 480 | return; |
| 481 | } |
| 482 | |
| 483 | VisualizedDeviceEntry* DevicesPane::getDeviceEntry() |
| 484 | { |
| 485 | hOutDebug(3,"getDeviceEntry - mLastClick=" << mLastClick); |
| 486 | std::map<int, VisualizedDeviceEntry>::iterator it = mDevices.begin(); |
| 487 | while(it != mDevices.end()) |
| 488 | { |
| 489 | if (it->second.getLineNumber() == mLastClick) |
| 490 | { |
| 491 | return &(it->second); |
| 492 | break; |
| 493 | } |
| 494 | it++; |
| 495 | } |
| 496 | return NULL; |
| 497 | } |
| 498 | |
| 499 | QString DevicesPane::textFromValue(int value) const |
| 500 | { |
| 501 | std::stringstream ss; |
| 502 | ss << std::hex << value; |
| 503 | char formatted[5]; |
| 504 | if (value <= 0xffff) |
| 505 | sprintf(formatted,"%4.4X",(value)); |
| 506 | else |
| 507 | strcpy(formatted,"0000"); |
| 508 | QString ret(formatted); |
| 509 | hOutDebug(5,"textFromValue:" << value << "='" << ret.toStdString()); |
| 510 | return ret; |
| 511 | } |