~kovax3/+junk/qwinwrap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/****************************************************************************
//   qwinwrap Copyright (C) 2012 yahia nouah <yahiaui@gmail.com>
//
//      This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
//      This is free software, and you are welcome to redistribute it
//      under certain conditions; type `show c' for details.
//
//  The hypothetical commands `show w' and `show c' should show the appropriate
//  parts of the General Public License.  Of course, your program's commands
//  might be different; for a GUI interface, you would use an "about box".
//
//    You should also get your employer (if you work as a programmer) or school,
//  if any, to sign a "copyright disclaimer" for the program, if necessary.
//  For more information on this, and how to apply and follow the GNU GPL, see
//  <http://www.gnu.org/licenses/>.
//
//    The GNU General Public License does not permit incorporating your program
//  into proprietary programs.  If your program is a subroutine library, you
//  may consider it more useful to permit linking proprietary applications with
//  the library.  If this is what you want to do, use the GNU Lesser General
//  Public License instead of this License.  But first, please read
//  <http://www.gnu.org/philosophy/why-not-lgpl.html>.
// ----------------------------------------------------------
** If you have questions regarding the use of this file, please contact
** yahia nouah (yahiaui@gmail.com)
** $qwinwrap_END_LICENSE$
**
****************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDir>
#include <QProcess>
#include <QMessageBox>
#include <QFileDialog>
#include <QSettings>
#include <QtGui>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    ui->listWidgetType->setViewMode(QListView::IconMode);
        ui->listWidgetType->setIconSize(QSize(64, 64));
        ui->listWidgetType->setMovement(QListView::Static);
        ui->listWidgetType->setMaximumWidth(100);
        ui->listWidgetType->setSpacing(12);
processDesktop=new QProcess;

if(QFile::exists("/usr/bin/xwinwrap")){
    m_xwinwrap="/usr/bin/xwinwrap";
    ui->radioButtonXwin->setToolTip("xwinwrap found in : "+m_xwinwrap);
  //  ui->labelXwin->setText("xwinwrap found in : "+m_xwinwrap);
}else if(QFile::exists(qApp->applicationDirPath()+"/xwinwrap")){
    m_xwinwrap="\""+qApp->applicationDirPath()+"/xwinwrap\"";
    ui->radioButtonXwin->setToolTip("xwinwrap found in : "+m_xwinwrap);
}else{
    ui->radioButtonXwin->setToolTip("xwinwrap no found" );
    ui->radioButtonXwin->setEnabled(false);
    ui->radioButtonWidget->setChecked(true);
}

//ui->widget->setVisible(false);

   loadLayout();
   createActions();
createTrayIcon();
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
        this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));

QDesktopWidget *wid=new QDesktopWidget;
    wid->setAttribute(Qt::WA_NoChildEventsFromChildren,true);

  //


    objDesktop=new ObjDesk;

//objDesktop->show();
        trayIcon->setIcon(QIcon(":/qwinwrap.png"));

// ui->listWidget->addItem("glmatrix");
    QDir dirS(m_xScreenSaverPath);
    QString subfile;
    foreach ( subfile, dirS.entryList(QDir::AllEntries| QDir::NoDotAndDotDot )){
         ui->listWidget->addItem(subfile);

    }
    listVo<<""<<"-vo gl_nosw"<<"-vo gl"<<"-vo gl2"<<"-vo matrixview";
ui->widget->setVisible(false);
  trayIcon->show();

     m_desktopId=findDesktop();
}

MainWindow::~MainWindow()
{
    delete ui;
     saveLayou();
}
void MainWindow::setVisible(bool visible)
{
    minimizeAction->setEnabled(visible);
    restoreAction->setEnabled(isMaximized() || !visible);
    QMainWindow::setVisible(visible);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
     on_pBtnKill_clicked();
}
void MainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    case QEvent::WindowStateChange:

        if(this->isMinimized()){

                 on_toolButtonPreview_toggled(false);
         }
          break;
    case QEvent::Hide:
      qDebug()<<this->isHidden();
        break;

    default:
        break;
    }
}



void MainWindow::on_pBtnApplique_clicked()
{

  on_pBtnKill_clicked();
    qreal i=ui->doubleSpinBox->value();
    QString opacity=QString::number(i);

    objDesktop->setWindowOpacity(i);
    if (ui->checkBoxSound->isChecked()){
        m_sound="-quiet";
    }else{
        m_sound="-nosound";
    }

    QString argument;
    if(ui->stackedWidget->currentIndex()==0){//xscreen saver
        if(!ui->listWidget->currentItem())return;
        QString pathScreenS=m_xScreenSaverPath+"/"+ui->listWidget->currentItem()->text();


        if(ui->radioButtonXwin->isChecked()){//xwinwrap
            argument=QString("%1 -ni -argb -o %2 -fs -s -st -sp  -b -nf  -- %3 -root -window-id WID "
                             ).arg(m_xwinwrap).arg(opacity).arg(pathScreenS);
        }else if(ui->radioButtonWidget->isChecked()){

            objDesktop->show();

            argument=QString("%1 -window-id  %2 "
                             ).arg(pathScreenS).arg(objDesktop->winId());
        }else if(ui->radioButtonDirect->isChecked()){
            argument=QString("%1 -root  -install -window-id  %2 "
                             ).arg(pathScreenS).arg(m_desktopId);
        }

    }else{ //mplayer
        m_vedioPath=ui->lineEdit->text().trimmed();
        int index=ui->comboBoxVo->currentIndex();
        QString strVo= listVo.at(index);

        if(ui->radioButtonXwin->isChecked()){//xwinwrap
                    if(ui->rBtnLoop->isChecked()){
                        QString loop=QString::number(ui->spinBoxLoop->value());
                        argument=QString("%1 -ni -o %2 -fs -s -st -sp -b -nf -- mplayer -wid WID %3  %4 -noconfig all -really-quiet -msglevel all=-1 -loop %5  1>/dev/null 2>/dev/null"
                                         ).arg(m_xwinwrap).arg(opacity).arg(m_sound).arg(strVo + " " +m_vedioPath).arg(loop);

                    }else{

                                argument=QString("%1 -ni -o %2 -fs -s -st -sp -b -nf -- mplayer -wid WID %3  %4 -noconfig all -really-quiet -msglevel all=-1 2>/dev/null"
                                                 ).arg(m_xwinwrap).arg(opacity).arg(m_sound).arg(strVo + " \"" +m_vedioPath+"\"");

                    }
        } else{
            objDesktop->setFileName(m_vedioPath,ui->checkBoxSound->checkState(),ui->rBtnLoop->checkState());
            objDesktop->show();
        }
    }
    qDebug()<<argument ;
    //   process.startDetached(argument);

    processDesktop->start(argument);


}


//void MainWindow::on_pBtnCancel_clicked()
//{
//        qApp->exit();
//}

void MainWindow::on_pBtnKill_clicked()
{
    QProcess process;
    process.execute("killall xwinwrap");
    process.close();

    processDesktop->close();
    objDesktop->hide();
    objDesktop->stopMedia();
}

void MainWindow::on_toolButton_clicked()
{

    QString fn = QFileDialog::getOpenFileName(this, tr("Open File..."),
                 QString(), trUtf8("Video Files (*)"));
    if (!fn.isEmpty())
    {
        ui->lineEdit->setText(fn);

    }
     //   ui->comboBox->setCurrentIndex(1);
}

void MainWindow::saveLayou()//حفظ البيانات الى ملف
{
    QSettings settings(QDir::homePath()+"/.Qwinwrap/qwinwrap.ini",
                       QSettings::IniFormat);

    settings.beginGroup("MainWindow");
    settings.setValue("xScreenSaverPath", m_xScreenSaverPath);
  settings.setValue("vedioPath", m_vedioPath);
    settings.endGroup();

}

void MainWindow::loadLayout()//load layou
{
     QSettings settings(QDir::homePath()+"/.Qwinwrap/qwinwrap.ini",
                        QSettings::IniFormat);
QString xScreenSaverPath;
     settings.beginGroup("MainWindow");
     if(QFile::exists("/usr/lib/xscreensaver")){
     xScreenSaverPath=settings.value("xScreenSaverPath","/usr/lib/xscreensaver").toString();
     }else  if(QFile::exists("/usr/lib64/xscreensaver")){
         xScreenSaverPath=settings.value("xScreenSaverPath","/usr/lib64/xscreensaver").toString();
    }else{
           xScreenSaverPath=qApp->applicationDirPath()+"/xscreensaver";
     }
     m_xScreenSaverPath=settings.value("xScreenSaverPath",xScreenSaverPath).toString();
     m_vedioPath=settings.value("vedioPath","").toString();
     ui->lineEdit->setText(m_vedioPath);
    settings.endGroup();
}

void MainWindow::on_actionSetScreenSaver_triggered()
{
    QString dirName = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                    QString(),
                                                    QFileDialog::ShowDirsOnly
                                                    |QFileDialog::DontResolveSymlinks);

    QDir dir;
    if(dir.exists(dirName)){
        m_xScreenSaverPath=dirName;
          ui->listWidget->clear();
                  QDir dirS(m_xScreenSaverPath);
        QString subfile;

        foreach ( subfile, dirS.entryList(QDir::AllEntries| QDir::NoDotAndDotDot )){
             ui->listWidget->addItem(subfile);
         //   filetemp.remove(tempDir+"/"+subfile);
        }
    }


}

void MainWindow::on_actionAboutQt_triggered()
{
    QMessageBox::aboutQt(this,"");
}

void MainWindow::on_actionAbout_triggered()
{
    QMessageBox::about(this,"","Qwinwrap application gui for xwinwrap \n"
                       "Author : abouzakaria\n"
                       "Version : " +QApplication::applicationVersion());
}
void MainWindow::createActions()
{
    playAction = new QAction(tr("play Animation"), this);
    connect(playAction, SIGNAL(triggered()), this, SLOT(on_pBtnApplique_clicked()));

    stopAction = new QAction(tr("Stop Animation"), this);
    connect(stopAction, SIGNAL(triggered()), this, SLOT(on_pBtnKill_clicked()));

    minimizeAction = new QAction(tr("Mi&nimize"), this);
    connect(minimizeAction, SIGNAL(triggered()), this, SLOT(showMinimized()));

    restoreAction = new QAction(tr("&Restore"), this);
    connect(restoreAction, SIGNAL(triggered()), this, SLOT(showNormal()));

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, SIGNAL(triggered()), this, SLOT(on_pBtnCancel_clicked()) );

}
void MainWindow::createTrayIcon()
{
    trayIconMenu = new QMenu(this);
 trayIconMenu->addAction(playAction);
    trayIconMenu->addAction(stopAction);
   trayIconMenu->addSeparator();
     trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason) {
    case QSystemTrayIcon::Trigger:
        if(this->isHidden()){
                this->showNormal();
        }else{
            this->hide();
             on_toolButtonPreview_toggled(false);
        }

    case QSystemTrayIcon::DoubleClick:

    case QSystemTrayIcon::MiddleClick:

    default:
        ;
    }
}

void MainWindow::on_listWidget_itemActivated(QListWidgetItem *item)
{

    on_pBtnApplique_clicked();

}

//void MainWindow::on_btnMinimize_clicked()
//{
//    hide();
//}

void MainWindow::on_actionQuit_triggered()
{
      on_pBtnKill_clicked();
qApp->quit();
}

void MainWindow::on_pBtnCancel_clicked()
{
     on_pBtnKill_clicked();
  qApp->quit();
     //  findDesktop();

}




void MainWindow::on_toolButtonPreview_toggled(bool checked)
{
    if(checked==false){
          processView.close();
           ui->widget->setVisible(false);
      //    ui->toolButtonPreview->setArrowType(Qt::LeftArrow);
ui->toolButtonPreview->setChecked(false);
    }else{
           ui->widget->setVisible(true);
         //    ui->toolButtonPreview->setArrowType(Qt::RightArrow);
             processView.close();
             if(!ui->listWidget->currentItem())
                 return;
             QString pathScreenS=m_xScreenSaverPath+"/"+ui->listWidget->currentItem()->text();

               QString    argument=QString("%1 -window-id %2 "
                                     ).arg(pathScreenS).arg(ui->widget->winId());

         qDebug( )<<argument;

         processView.start(argument);
    }
}

void MainWindow::on_listWidget_itemSelectionChanged()
{
    if(!ui->widget->isVisible())
        return;
    processView.close();


QString pathScreenS=m_xScreenSaverPath+"/"+ui->listWidget->currentItem()->text();
//process.startDetached(m_xScreenSaverPath+"/"+item->text());
      QString    argument=QString("%1 -window-id  %2 "
                            ).arg(pathScreenS).arg(ui->widget->winId());

qDebug( )<<argument;

processView.start(argument);
}

void MainWindow::on_widget_customContextMenuRequested(const QPoint &pos)
{
    ui->menuFile->exec(this->cursor().pos());
}

QString MainWindow::findDesktop()
{
    int W=QApplication::desktop()->geometry().width();
    int Y=QApplication::desktop()->geometry().height();
    QString G=QString::number(W)+"x"+QString::number(Y)+"+0+0";
    qDebug()<<"this geometry= "+G;
    QProcess p;

    p.start("xwininfo -root -tree");
    p.waitForFinished();
     QByteArray result = p.readAll();
     QTextCodec *codec = QTextCodec::codecForName("UTF-8");
       QString string = codec->toUnicode(result);
       QTextStream inw(&string);

       QString thisLine;
       bool isFirsChild=false;
       bool asChild=true;
       bool asdesktop=false;
       while ( !inw.atEnd() )
       {
          QString widthline = inw.readLine();
          if(widthline.contains("child")){
            isFirsChild=true;
              asChild=true;
          }else{

                asChild=false;
          }

          if(widthline.contains(G)){
                if(widthline.contains("desktop"))
                 asdesktop=true;

                if(isFirsChild==true&&asChild==false&&asdesktop==true)
                       thisLine=widthline.trimmed();
}
         //  qDebug()<<"isFirsChild : "<<isFirsChild <<" asChild : "<<asChild <<"   asdesktop : "<<asdesktop<<  "  : "<<widthline;
           if(!widthline.contains("child"))
                isFirsChild=false;

       }
      //   QMessageBox::information(0,"",thisLine);
         QString id=thisLine.section(" ",0,0);

return id;

}

void MainWindow::getid()
{
    bool ok;
        QString text = QInputDialog::getText(this, tr("QInputDialog::getText()"),
                                             tr("User name:"), QLineEdit::Normal,
                                             m_desktopId, &ok);
        if (ok && !text.isEmpty())
          m_desktopId=text;
  //    QMessageBox::information(0,"",findDesktop());
}

void MainWindow::on_btnMinimize_clicked()
{
    this->hide();
    on_toolButtonPreview_toggled(false);
}

void MainWindow::on_stackedWidget_currentChanged(int arg1)
{
    if(arg1==1)
           on_toolButtonPreview_toggled(false);
}