-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
129 lines (113 loc) · 3.95 KB
/
Copy pathmain.cpp
File metadata and controls
129 lines (113 loc) · 3.95 KB
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
// The main can be compiled using qmack and the provided .pro file to produce a makefile
// The make file can then be used to finish the linking
// QT 5.14.1 is a prerequiste for building and (execution for now)
// There is a separate executeable file disk_analyzer but it links dynamically to Qt libraries
// Remeber to pass the desired starting path or it will start form {pwd}
#define _XOPEN_SOURCE 500
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QTreeView>
#include <QtWidgets/QSplitter>
#include <QtCharts/QChartView>
#include <QtGui/QScreen>
#include "piechart.h"
#include "treemodel.h"
#include "tree.h"
#define HEIGHT 500
#define WIDTH 1000
QT_CHARTS_USE_NAMESPACE
int glb_lvl = 0;
TREE *tree;
// the function that will be passed to the nftw system call to fetch the file tree
int
append_tree(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf);
// a function to be used with post order traversal to adjust the size of the tree
// nftw sys call returns the size of a directory as 4 KB ignoring its content
// this recursive fucnction adjusts the sizes of directories
int
set_size(NODE *node);
int
main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Backend Part
// Making the tree
tree = make_tree();
int flags = 0;
flags |= FTW_PHYS;
// Traversing the file system
if (nftw((argc < 2) ? "." : argv[1], append_tree, 20, flags) == -1) {
perror("nftw");
}
traverse_post(tree, set_size);
// Frontend Part
// initializing data for models
PieChart *pie = new PieChart();
pie->setNode(tree->root);
TreeModel model(tree);
// Making the chart area
QChartView *chartView = new QChartView(pie);
chartView->setRenderHint(QPainter::Antialiasing);
// Making the tree area
QTreeView *treeView = new QTreeView;
treeView->setModel(&model);
treeView->setWindowTitle(QObject::tr("Disk Analyzer"));
treeView->setAnimated(true);
treeView->setIndentation(20);
const QSize availableSize = treeView->screen()->availableGeometry().size();
treeView->resize(availableSize / 2);
treeView->setSortingEnabled(false);
treeView->sortByColumn(1,Qt::DescendingOrder);
// Filling the main window
QSplitter *splitter = new QSplitter;
splitter->addWidget(treeView);
splitter->addWidget(chartView);
QList<int> list;
list.append(WIDTH/3);
list.append(2*(WIDTH/3));
splitter->setSizes(list);
QMainWindow window;
//window.connect(treeView,treeView->expanded(treeView->selectionModel()->currentIndex()),chartView, chartView->)
window.setCentralWidget(splitter);
window.resize(WIDTH, HEIGHT);
window.show();
return a.exec();
}
int
append_tree(const char *fpath, const struct stat *sb,
int tflag, struct FTW *ftwbuf)
{
char *name = strcat((char *)fpath + ftwbuf->base, "\0");
char *name2 = (char *)malloc((strlen(name) + 1) * sizeof(char));
strcpy(name2, name);
NODE *node = make_node(tflag,
ftwbuf->level,
sb->st_size,
name2);
if (ftwbuf->level > glb_lvl || tree->root == NULL) {
add_child(node, tree);
} else if (ftwbuf->level == glb_lvl) {
tree->current = tree->current->parent;
add_child(node, tree);
} else if (ftwbuf->level < glb_lvl) {
for (int i = 0; i < (glb_lvl - ftwbuf->level) + 1; i++)
tree->current = tree->current->parent;
add_child(node, tree);
}
tree->current = node;
glb_lvl = ftwbuf->level;
return 0;
}
int
set_size(NODE *node)
{
for (int i = 0; i < node->child_count; i++)
node->size += node->childs[i]->size;
return 0;
}