From 253067c782955380bbf66ac0475dc954375b1ff4 Mon Sep 17 00:00:00 2001 From: Petr Mrázek Date: Sat, 17 Aug 2013 13:40:51 +0200 Subject: Move all the things (YES. Move them.) Also, implemented some basic modlist logic, to be wired up. --- depends/classparser/CMakeLists.txt | 41 + depends/classparser/include/classparser_config.h | 23 + depends/classparser/include/javautils.h | 29 + depends/classparser/src/annotations.cpp | 83 + depends/classparser/src/annotations.h | 252 +++ depends/classparser/src/classfile.h | 153 ++ depends/classparser/src/constants.h | 212 +++ depends/classparser/src/errors.h | 6 + depends/classparser/src/javaendian.h | 62 + depends/classparser/src/javautils.cpp | 81 + depends/classparser/src/membuffer.h | 64 + depends/groupview/CMakeLists.txt | 41 + .../include/categorizedsortfilterproxymodel.h | 175 ++ depends/groupview/include/categorizedview.h | 332 ++++ depends/groupview/include/categorydrawer.h | 179 ++ depends/groupview/include/groupview_config.h | 27 + .../src/categorizedsortfilterproxymodel.cpp | 168 ++ .../src/categorizedsortfilterproxymodel_p.h | 48 + depends/groupview/src/categorizedview.cpp | 1713 ++++++++++++++++++++ depends/groupview/src/categorizedview_p.h | 159 ++ depends/groupview/src/categorydrawer.cpp | 231 +++ depends/launcher/CMakeLists.txt | 23 + depends/launcher/MCFrame.java | 123 ++ depends/launcher/MultiMCLauncher.java | 331 ++++ depends/launcher/UseJava.cmake | 881 ++++++++++ depends/launcher/UseJavaClassFilelist.cmake | 52 + depends/launcher/UseJavaSymlinks.cmake | 32 + depends/launcher/net/minecraft/Launcher.java | 154 ++ .../org/simplericity/macify/eawt/Application.java | 176 ++ .../macify/eawt/ApplicationAdapter.java | 48 + .../simplericity/macify/eawt/ApplicationEvent.java | 25 + .../macify/eawt/ApplicationListener.java | 27 + .../macify/eawt/DefaultApplication.java | 418 +++++ depends/patchlib/CMakeLists.txt | 14 + depends/patchlib/LICENSE-bzip2 | 42 + depends/patchlib/blocksort.c | 1095 +++++++++++++ depends/patchlib/bspatch.c | 303 ++++ depends/patchlib/bspatch.h | 27 + depends/patchlib/bzlib.c | 1579 ++++++++++++++++++ depends/patchlib/bzlib.h | 283 ++++ depends/patchlib/bzlib_private.h | 510 ++++++ depends/patchlib/compress.c | 672 ++++++++ depends/patchlib/crctable.c | 104 ++ depends/patchlib/decompress.c | 646 ++++++++ depends/patchlib/huffman.c | 205 +++ depends/patchlib/randtable.c | 84 + depends/quazip/CMakeLists.txt | 28 + depends/quazip/JlCompress.cpp | 516 ++++++ depends/quazip/JlCompress.h | 123 ++ depends/quazip/crypt.h | 135 ++ depends/quazip/ioapi.h | 77 + depends/quazip/qioapi.cpp | 146 ++ depends/quazip/quaadler32.cpp | 28 + depends/quazip/quaadler32.h | 29 + depends/quazip/quachecksum32.h | 54 + depends/quazip/quacrc32.cpp | 28 + depends/quazip/quacrc32.h | 26 + depends/quazip/quagzipfile.cpp | 141 ++ depends/quazip/quagzipfile.h | 35 + depends/quazip/quaziodevice.cpp | 283 ++++ depends/quazip/quaziodevice.h | 27 + depends/quazip/quazip.cpp | 554 +++++++ depends/quazip/quazip.h | 411 +++++ depends/quazip/quazip_global.h | 55 + depends/quazip/quazipdir.cpp | 507 ++++++ depends/quazip/quazipdir.h | 171 ++ depends/quazip/quazipfile.cpp | 488 ++++++ depends/quazip/quazipfile.h | 442 +++++ depends/quazip/quazipfileinfo.h | 66 + depends/quazip/quazipnewinfo.cpp | 51 + depends/quazip/quazipnewinfo.h | 102 ++ depends/quazip/unzip.c | 1603 ++++++++++++++++++ depends/quazip/unzip.h | 356 ++++ depends/quazip/zip.c | 1281 +++++++++++++++ depends/quazip/zip.h | 245 +++ depends/settings/CMakeLists.txt | 51 + depends/settings/include/basicsettingsobject.h | 44 + depends/settings/include/inifile.h | 38 + depends/settings/include/inisettingsobject.h | 60 + depends/settings/include/keyring.h | 92 ++ depends/settings/include/libsettings_config.h | 27 + depends/settings/include/overridesetting.h | 43 + depends/settings/include/setting.h | 114 ++ depends/settings/include/settingsobject.h | 192 +++ depends/settings/src/basicsettingsobject.cpp | 46 + depends/settings/src/inifile.cpp | 86 + depends/settings/src/inisettingsobject.cpp | 62 + depends/settings/src/keyring.cpp | 63 + depends/settings/src/overridesetting.cpp | 30 + depends/settings/src/setting.cpp | 54 + depends/settings/src/settingsobject.cpp | 144 ++ depends/settings/src/stubkeyring.cpp | 104 ++ depends/settings/src/stubkeyring.h | 41 + depends/util/CMakeLists.txt | 51 + depends/util/include/apputils.h | 21 + depends/util/include/cmdutils.h | 259 +++ depends/util/include/libutil_config.h | 27 + depends/util/include/osutils.h | 29 + depends/util/include/pathutils.h | 37 + depends/util/include/siglist.h | 129 ++ depends/util/include/siglist_impl.h | 156 ++ depends/util/include/userutils.h | 19 + depends/util/src/cmdutils.cpp | 484 ++++++ depends/util/src/osutils.cpp | 19 + depends/util/src/pathutils.cpp | 94 ++ depends/util/src/userutils.cpp | 123 ++ 106 files changed, 22680 insertions(+) create mode 100644 depends/classparser/CMakeLists.txt create mode 100644 depends/classparser/include/classparser_config.h create mode 100644 depends/classparser/include/javautils.h create mode 100644 depends/classparser/src/annotations.cpp create mode 100644 depends/classparser/src/annotations.h create mode 100644 depends/classparser/src/classfile.h create mode 100644 depends/classparser/src/constants.h create mode 100644 depends/classparser/src/errors.h create mode 100644 depends/classparser/src/javaendian.h create mode 100644 depends/classparser/src/javautils.cpp create mode 100644 depends/classparser/src/membuffer.h create mode 100644 depends/groupview/CMakeLists.txt create mode 100644 depends/groupview/include/categorizedsortfilterproxymodel.h create mode 100644 depends/groupview/include/categorizedview.h create mode 100644 depends/groupview/include/categorydrawer.h create mode 100644 depends/groupview/include/groupview_config.h create mode 100644 depends/groupview/src/categorizedsortfilterproxymodel.cpp create mode 100644 depends/groupview/src/categorizedsortfilterproxymodel_p.h create mode 100644 depends/groupview/src/categorizedview.cpp create mode 100644 depends/groupview/src/categorizedview_p.h create mode 100644 depends/groupview/src/categorydrawer.cpp create mode 100644 depends/launcher/CMakeLists.txt create mode 100644 depends/launcher/MCFrame.java create mode 100644 depends/launcher/MultiMCLauncher.java create mode 100644 depends/launcher/UseJava.cmake create mode 100644 depends/launcher/UseJavaClassFilelist.cmake create mode 100644 depends/launcher/UseJavaSymlinks.cmake create mode 100644 depends/launcher/net/minecraft/Launcher.java create mode 100644 depends/launcher/org/simplericity/macify/eawt/Application.java create mode 100644 depends/launcher/org/simplericity/macify/eawt/ApplicationAdapter.java create mode 100644 depends/launcher/org/simplericity/macify/eawt/ApplicationEvent.java create mode 100644 depends/launcher/org/simplericity/macify/eawt/ApplicationListener.java create mode 100644 depends/launcher/org/simplericity/macify/eawt/DefaultApplication.java create mode 100644 depends/patchlib/CMakeLists.txt create mode 100644 depends/patchlib/LICENSE-bzip2 create mode 100644 depends/patchlib/blocksort.c create mode 100644 depends/patchlib/bspatch.c create mode 100644 depends/patchlib/bspatch.h create mode 100644 depends/patchlib/bzlib.c create mode 100644 depends/patchlib/bzlib.h create mode 100644 depends/patchlib/bzlib_private.h create mode 100644 depends/patchlib/compress.c create mode 100644 depends/patchlib/crctable.c create mode 100644 depends/patchlib/decompress.c create mode 100644 depends/patchlib/huffman.c create mode 100644 depends/patchlib/randtable.c create mode 100644 depends/quazip/CMakeLists.txt create mode 100644 depends/quazip/JlCompress.cpp create mode 100644 depends/quazip/JlCompress.h create mode 100644 depends/quazip/crypt.h create mode 100644 depends/quazip/ioapi.h create mode 100644 depends/quazip/qioapi.cpp create mode 100644 depends/quazip/quaadler32.cpp create mode 100644 depends/quazip/quaadler32.h create mode 100644 depends/quazip/quachecksum32.h create mode 100644 depends/quazip/quacrc32.cpp create mode 100644 depends/quazip/quacrc32.h create mode 100644 depends/quazip/quagzipfile.cpp create mode 100644 depends/quazip/quagzipfile.h create mode 100644 depends/quazip/quaziodevice.cpp create mode 100644 depends/quazip/quaziodevice.h create mode 100644 depends/quazip/quazip.cpp create mode 100644 depends/quazip/quazip.h create mode 100644 depends/quazip/quazip_global.h create mode 100644 depends/quazip/quazipdir.cpp create mode 100644 depends/quazip/quazipdir.h create mode 100644 depends/quazip/quazipfile.cpp create mode 100644 depends/quazip/quazipfile.h create mode 100644 depends/quazip/quazipfileinfo.h create mode 100644 depends/quazip/quazipnewinfo.cpp create mode 100644 depends/quazip/quazipnewinfo.h create mode 100644 depends/quazip/unzip.c create mode 100644 depends/quazip/unzip.h create mode 100644 depends/quazip/zip.c create mode 100644 depends/quazip/zip.h create mode 100644 depends/settings/CMakeLists.txt create mode 100644 depends/settings/include/basicsettingsobject.h create mode 100644 depends/settings/include/inifile.h create mode 100644 depends/settings/include/inisettingsobject.h create mode 100644 depends/settings/include/keyring.h create mode 100644 depends/settings/include/libsettings_config.h create mode 100644 depends/settings/include/overridesetting.h create mode 100644 depends/settings/include/setting.h create mode 100644 depends/settings/include/settingsobject.h create mode 100644 depends/settings/src/basicsettingsobject.cpp create mode 100644 depends/settings/src/inifile.cpp create mode 100644 depends/settings/src/inisettingsobject.cpp create mode 100644 depends/settings/src/keyring.cpp create mode 100644 depends/settings/src/overridesetting.cpp create mode 100644 depends/settings/src/setting.cpp create mode 100644 depends/settings/src/settingsobject.cpp create mode 100644 depends/settings/src/stubkeyring.cpp create mode 100644 depends/settings/src/stubkeyring.h create mode 100644 depends/util/CMakeLists.txt create mode 100644 depends/util/include/apputils.h create mode 100644 depends/util/include/cmdutils.h create mode 100644 depends/util/include/libutil_config.h create mode 100644 depends/util/include/osutils.h create mode 100644 depends/util/include/pathutils.h create mode 100644 depends/util/include/siglist.h create mode 100644 depends/util/include/siglist_impl.h create mode 100644 depends/util/include/userutils.h create mode 100644 depends/util/src/cmdutils.cpp create mode 100644 depends/util/src/osutils.cpp create mode 100644 depends/util/src/pathutils.cpp create mode 100644 depends/util/src/userutils.cpp (limited to 'depends') diff --git a/depends/classparser/CMakeLists.txt b/depends/classparser/CMakeLists.txt new file mode 100644 index 00000000..5a48e002 --- /dev/null +++ b/depends/classparser/CMakeLists.txt @@ -0,0 +1,41 @@ +project(classparser) + +set(CMAKE_AUTOMOC ON) + +# Find Qt +find_package(Qt5Core REQUIRED) + +# Include Qt headers. +include_directories(${Qt5Base_INCLUDE_DIRS}) + +SET(CLASSPARSER_HEADERS +include/classparser_config.h + +# Public headers +include/javautils.h + +# Private headers +src/annotations.h +src/classfile.h +src/constants.h +src/errors.h +src/javaendian.h +src/membuffer.h +) + +SET(CLASSPARSER_SOURCES +src/javautils.cpp +src/annotations.cpp +) + +# Set the include dir path. +SET(LIBGROUPVIEW_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE) + +# Include self. +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) +include_directories(${CMAKE_BINARY_DIR}/include) + +add_definitions(-DCLASSPARSER_LIBRARY) + +add_library(classparser SHARED ${CLASSPARSER_SOURCES} ${CLASSPARSER_HEADERS}) +qt5_use_modules(classparser Core) diff --git a/depends/classparser/include/classparser_config.h b/depends/classparser/include/classparser_config.h new file mode 100644 index 00000000..fe6a2ab9 --- /dev/null +++ b/depends/classparser/include/classparser_config.h @@ -0,0 +1,23 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#ifdef CLASSPARSER_LIBRARY +# define CLASSPARSER_EXPORT Q_DECL_EXPORT +#else +# define CLASSPARSER_EXPORT Q_DECL_IMPORT +#endif + diff --git a/depends/classparser/include/javautils.h b/depends/classparser/include/javautils.h new file mode 100644 index 00000000..63e5ec26 --- /dev/null +++ b/depends/classparser/include/javautils.h @@ -0,0 +1,29 @@ +/* Copyright 2013 MultiMC Contributors + * + * Authors: Orochimarufan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#pragma once +#include +#include "classparser_config.h" + +#define MCVer_Unknown "Unknown" + +namespace javautils +{ + /** + * @brief Get the version from a minecraft.jar by parsing its class files. Expensive! + */ + QString GetMinecraftJarVersion(QString jar); +} diff --git a/depends/classparser/src/annotations.cpp b/depends/classparser/src/annotations.cpp new file mode 100644 index 00000000..fc0c98fa --- /dev/null +++ b/depends/classparser/src/annotations.cpp @@ -0,0 +1,83 @@ +#include "classfile.h" +#include "annotations.h" +#include + +namespace java +{ + std::string annotation::toString() + { + std::ostringstream ss; + ss << "Annotation type : " << type_index << " - " << pool[type_index].str_data << std::endl; + ss << "Contains " << name_val_pairs.size() << " pairs:" << std::endl; + for(unsigned i = 0; i < name_val_pairs.size(); i++) + { + std::pair &val = name_val_pairs[i]; + auto name_idx = val.first; + ss << pool[name_idx].str_data << "(" << name_idx << ")" << " = " << val.second->toString() << std::endl; + } + return ss.str(); + } + + annotation * annotation::read (util::membuffer& input, constant_pool& pool) + { + uint16_t type_index = 0; + input.read_be(type_index); + annotation * ann = new annotation(type_index,pool); + + uint16_t num_pairs = 0; + input.read_be(num_pairs); + while(num_pairs) + { + uint16_t name_idx = 0; + // read name index + input.read_be(name_idx); + auto elem = element_value::readElementValue(input,pool); + // read value + ann->add_pair(name_idx, elem); + num_pairs --; + } + return ann; + } + + element_value* element_value::readElementValue ( util::membuffer& input, java::constant_pool& pool ) + { + element_value_type type = INVALID; + input.read(type); + uint16_t index = 0; + uint16_t index2 = 0; + std::vector vals; + switch (type) + { + case PRIMITIVE_BYTE: + case PRIMITIVE_CHAR: + case PRIMITIVE_DOUBLE: + case PRIMITIVE_FLOAT: + case PRIMITIVE_INT: + case PRIMITIVE_LONG: + case PRIMITIVE_SHORT: + case PRIMITIVE_BOOLEAN: + case STRING: + input.read_be(index); + return new element_value_simple(type, index, pool); + case ENUM_CONSTANT: + input.read_be(index); + input.read_be(index2); + return new element_value_enum(type, index, index2, pool); + case CLASS: // Class + input.read_be(index); + return new element_value_class(type, index, pool); + case ANNOTATION: // Annotation + // FIXME: runtime visibility info needs to be passed from parent + return new element_value_annotation(ANNOTATION, annotation::read(input, pool), pool); + case ARRAY: // Array + input.read_be(index); + for (int i = 0; i < index; i++) + { + vals.push_back(element_value::readElementValue(input, pool)); + } + return new element_value_array(ARRAY, vals, pool); + default: + throw new java::classfile_exception(); + } + } +} \ No newline at end of file diff --git a/depends/classparser/src/annotations.h b/depends/classparser/src/annotations.h new file mode 100644 index 00000000..b115dc0b --- /dev/null +++ b/depends/classparser/src/annotations.h @@ -0,0 +1,252 @@ +#pragma once +#include "classfile.h" +#include +#include + +namespace java +{ + enum element_value_type : uint8_t + { + INVALID = 0, + STRING = 's', + ENUM_CONSTANT = 'e', + CLASS = 'c', + ANNOTATION = '@', + ARRAY = '[', // one array dimension + PRIMITIVE_INT = 'I', // integer + PRIMITIVE_BYTE = 'B', // signed byte + PRIMITIVE_CHAR = 'C', // Unicode character code point in the Basic Multilingual Plane, encoded with UTF-16 + PRIMITIVE_DOUBLE = 'D', // double-precision floating-point value + PRIMITIVE_FLOAT = 'F', // single-precision floating-point value + PRIMITIVE_LONG = 'J', // long integer + PRIMITIVE_SHORT = 'S', // signed short + PRIMITIVE_BOOLEAN = 'Z' // true or false + }; + /** + * The element_value structure is a discriminated union representing the value of an element-value pair. + * It is used to represent element values in all attributes that describe annotations + * - RuntimeVisibleAnnotations + * - RuntimeInvisibleAnnotations + * - RuntimeVisibleParameterAnnotations + * - RuntimeInvisibleParameterAnnotations). + * + * The element_value structure has the following format: + */ + class element_value + { + protected: + element_value_type type; + constant_pool & pool; + + public: + element_value(element_value_type type, constant_pool & pool): type(type), pool(pool) {}; + + element_value_type getElementValueType() + { + return type; + } + + virtual std::string toString() = 0; + + static element_value * readElementValue(util::membuffer & input, constant_pool & pool); + }; + + /** + * Each value of the annotations table represents a single runtime-visible annotation on a program element. + * The annotation structure has the following format: + */ + class annotation + { + public: + typedef std::vector< std::pair > value_list; + protected: + /** + * The value of the type_index item must be a valid index into the constant_pool table. + * The constant_pool entry at that index must be a CONSTANT_Utf8_info (§4.4.7) structure + * representing a field descriptor representing the annotation type corresponding + * to the annotation represented by this annotation structure. + */ + uint16_t type_index; + /** + * map between element_name_index and value. + * + * The value of the element_name_index item must be a valid index into the constant_pool table. + * The constant_pool entry at that index must be a CONSTANT_Utf8_info (§4.4.7) structure representing + * a valid field descriptor (§4.3.2) that denotes the name of the annotation type element represented + * by this element_value_pairs entry. + */ + value_list name_val_pairs; + /** + * Reference to the parent constant pool + */ + constant_pool & pool; + public: + annotation(uint16_t type_index, constant_pool& pool):type_index(type_index), pool(pool) {}; + ~annotation() + { + for(unsigned i = 0 ; i < name_val_pairs.size(); i++) + { + delete name_val_pairs[i].second; + } + } + void add_pair(uint16_t key, element_value * value) + { + name_val_pairs.push_back(std::make_pair(key, value)); + }; + value_list::const_iterator begin() + { + return name_val_pairs.cbegin(); + } + value_list::const_iterator end() + { + return name_val_pairs.cend(); + } + std::string toString(); + static annotation * read(util::membuffer & input, constant_pool & pool); + }; + typedef std::vector annotation_table; + + + /// type for simple value annotation elements + class element_value_simple : public element_value + { + protected: + /// index of the constant in the constant pool + uint16_t index; + public: + element_value_simple(element_value_type type, uint16_t index , constant_pool& pool): + element_value(type, pool), index(index) + { + // TODO: verify consistency + }; + uint16_t getIndex() + { + return index; + } + virtual std::string toString() + { + return pool[index].toString(); + }; + }; + /// The enum_const_value item is used if the tag item is 'e'. + class element_value_enum : public element_value + { + protected: + /** + * The value of the type_name_index item must be a valid index into the constant_pool table. + * The constant_pool entry at that index must be a CONSTANT_Utf8_info (§4.4.7) structure + * representing a valid field descriptor (§4.3.2) that denotes the internal form of the binary + * name (§4.2.1) of the type of the enum constant represented by this element_value structure. + */ + uint16_t typeIndex; + /** + * The value of the const_name_index item must be a valid index into the constant_pool table. + * The constant_pool entry at that index must be a CONSTANT_Utf8_info (§4.4.7) structure + * representing the simple name of the enum constant represented by this element_value structure. + */ + uint16_t valueIndex; + public: + element_value_enum(element_value_type type, uint16_t typeIndex, uint16_t valueIndex, constant_pool& pool): + element_value(type, pool), typeIndex(typeIndex), valueIndex(valueIndex) + { + // TODO: verify consistency + } + uint16_t getValueIndex() + { + return valueIndex; + } + uint16_t getTypeIndex() + { + return typeIndex; + } + virtual std::string toString() + { + return "enum value"; + }; + }; + + class element_value_class : public element_value + { + protected: + /** + * The class_info_index item must be a valid index into the constant_pool table. + * The constant_pool entry at that index must be a CONSTANT_Utf8_info (§4.4.7) structure + * representing the return descriptor (§4.3.3) of the type that is reified by the class + * represented by this element_value structure. + * + * For example, 'V' for Void.class, 'Ljava/lang/Object;' for Object, etc. + * + * Or in plain english, you can store type information in annotations. Yay. + */ + uint16_t classIndex; + public: + element_value_class(element_value_type type, uint16_t classIndex, constant_pool& pool): + element_value(type, pool), classIndex(classIndex) + { + // TODO: verify consistency + } + uint16_t getIndex() + { + return classIndex; + } + virtual std::string toString() + { + return "class"; + }; + }; + + /// nested annotations... yay + class element_value_annotation : public element_value + { + private: + annotation * nestedAnnotation; + public: + element_value_annotation(element_value_type type, annotation * nestedAnnotation, constant_pool& pool): + element_value(type, pool), nestedAnnotation(nestedAnnotation) + {}; + ~element_value_annotation() + { + if(nestedAnnotation) + { + delete nestedAnnotation; + nestedAnnotation = nullptr; + } + } + virtual std::string toString() + { + return "nested annotation"; + }; + }; + + /// and arrays! + class element_value_array : public element_value + { + public: + typedef std::vector elem_vec; + protected: + elem_vec values; + public: + element_value_array ( element_value_type type, std::vector & values, constant_pool& pool ): + element_value(type, pool), values(values) + {}; + ~element_value_array () + { + for(unsigned i = 0; i < values.size();i++) + { + delete values[i]; + } + }; + elem_vec::const_iterator begin() + { + return values.cbegin(); + } + elem_vec::const_iterator end() + { + return values.cend(); + } + virtual std::string toString() + { + return "array"; + }; + }; +} \ No newline at end of file diff --git a/depends/classparser/src/classfile.h b/depends/classparser/src/classfile.h new file mode 100644 index 00000000..33207e99 --- /dev/null +++ b/depends/classparser/src/classfile.h @@ -0,0 +1,153 @@ +#pragma once +#include "membuffer.h" +#include "constants.h" +#include "annotations.h" +#include +namespace java +{ + /** + * Class representing a Java .class file + */ + class classfile : public util::membuffer + { + public: + classfile(char * data, std::size_t size) : membuffer(data, size) + { + valid = false; + is_synthetic = false; + read_be(magic); + if(magic != 0xCAFEBABE) + throw new classfile_exception(); + read_be(minor_version); + read_be(major_version); + constants.load(*this); + read_be(access_flags); + read_be(this_class); + read_be(super_class); + + // Interfaces + uint16_t iface_count = 0; + read_be(iface_count); + while (iface_count) + { + uint16_t iface; + read_be(iface); + interfaces.push_back(iface); + iface_count --; + } + + // Fields + // read fields (and attributes from inside fields) (and possible inner classes. yay for recursion!) + // for now though, we will ignore all attributes + /* + * field_info + * { + * u2 access_flags; + * u2 name_index; + * u2 descriptor_index; + * u2 attributes_count; + * attribute_info attributes[attributes_count]; + * } + */ + uint16_t field_count = 0; + read_be(field_count); + while (field_count) + { + // skip field stuff + skip(6); + // and skip field attributes + uint16_t attr_count = 0; + read_be(attr_count); + while(attr_count) + { + skip(2); + uint32_t attr_length = 0; + read_be(attr_length); + skip(attr_length); + attr_count --; + } + field_count --; + } + + // class methods + /* + * method_info + * { + * u2 access_flags; + * u2 name_index; + * u2 descriptor_index; + * u2 attributes_count; + * attribute_info attributes[attributes_count]; + * } + */ + uint16_t method_count = 0; + read_be(method_count); + while( method_count ) + { + skip(6); + // and skip method attributes + uint16_t attr_count = 0; + read_be(attr_count); + while(attr_count) + { + skip(2); + uint32_t attr_length = 0; + read_be(attr_length); + skip(attr_length); + attr_count --; + } + method_count --; + } + + // class attributes + // there are many kinds of attributes. this is just the generic wrapper structure. + // type is decided by attribute name. extensions to the standard are *possible* + // class annotations are one kind of a attribute (one per class) + /* + * attribute_info + * { + * u2 attribute_name_index; + * u4 attribute_length; + * u1 info[attribute_length]; + * } + */ + uint16_t class_attr_count = 0; + read_be(class_attr_count); + while(class_attr_count) + { + uint16_t name_idx = 0; + read_be(name_idx); + uint32_t attr_length = 0; + read_be(attr_length); + + auto name = constants[name_idx]; + if(name.str_data == "RuntimeVisibleAnnotations") + { + uint16_t num_annotations = 0; + read_be(num_annotations); + while (num_annotations) + { + visible_class_annotations.push_back(annotation::read(*this, constants)); + num_annotations --; + } + } + else skip(attr_length); + class_attr_count --; + } + valid = true; + }; + bool valid; + bool is_synthetic; + uint32_t magic; + uint16_t minor_version; + uint16_t major_version; + constant_pool constants; + uint16_t access_flags; + uint16_t this_class; + uint16_t super_class; + // interfaces this class implements ? must be. investigate. + std::vector interfaces; + // FIXME: doesn't free up memory on delete + java::annotation_table visible_class_annotations; + }; +} \ No newline at end of file diff --git a/depends/classparser/src/constants.h b/depends/classparser/src/constants.h new file mode 100644 index 00000000..61aa5687 --- /dev/null +++ b/depends/classparser/src/constants.h @@ -0,0 +1,212 @@ +#pragma once +#include "errors.h" +#include + +namespace java +{ + class constant + { + public: + enum type_t : uint8_t + { + j_hole = 0, // HACK: this is a hole in the array, because java is crazy + j_string_data = 1, + j_int = 3, + j_float = 4, + j_long = 5, + j_double = 6, + j_class = 7, + j_string = 8, + j_fieldref = 9, + j_methodref = 10, + j_interface_methodref = 11, + j_nameandtype = 12 + } type; + + constant(util::membuffer & buf ) + { + buf.read(type); + // invalid constant type! + if(type > j_nameandtype || type == (type_t)0 || type == (type_t)2) + throw new classfile_exception(); + + // load data depending on type + switch(type) + { + case j_float: + case j_int: + buf.read_be(int_data); // same as float data really + break; + case j_double: + case j_long: + buf.read_be(long_data); // same as double + break; + case j_class: + buf.read_be(ref_type.class_idx); + break; + case j_fieldref: + case j_methodref: + case j_interface_methodref: + buf.read_be(ref_type.class_idx); + buf.read_be(ref_type.name_and_type_idx); + break; + case j_string: + buf.read_be(index); + break; + case j_string_data: + // HACK HACK: for now, we call these UTF-8 and do no further processing. + // Later, we should do some decoding. It's really modified UTF-8 + // * U+0000 is represented as 0xC0,0x80 invalid character + // * any single zero byte ends the string + // * characters above U+10000 are encoded like in CESU-8 + buf.read_jstr(str_data); + break; + case j_nameandtype: + buf.read_be(name_and_type.name_index); + buf.read_be(name_and_type.descriptor_index); + break; + } + } + + constant(int fake) + { + type = j_hole; + } + + std::string toString() + { + std::ostringstream ss; + switch(type) + { + case j_hole: + ss << "Fake legacy entry"; + break; + case j_float: + ss << "Float: " << float_data; + break; + case j_double: + ss << "Double: " << double_data; + break; + case j_int: + ss << "Int: " << int_data; + break; + case j_long: + ss << "Long: " << long_data; + break; + case j_string_data: + ss << "StrData: " << str_data; + break; + case j_string: + ss << "Str: " << index; + break; + case j_fieldref: + ss << "FieldRef: " << ref_type.class_idx << " " << ref_type.name_and_type_idx; + break; + case j_methodref: + ss << "MethodRef: " << ref_type.class_idx << " " << ref_type.name_and_type_idx; + break; + case j_interface_methodref: + ss << "IfMethodRef: " << ref_type.class_idx << " " << ref_type.name_and_type_idx; + break; + case j_class: + ss << "Class: " << ref_type.class_idx; + break; + case j_nameandtype: + ss << "NameAndType: " << name_and_type.name_index << " " << name_and_type.descriptor_index; + break; + } + return ss.str(); + } + + std::string str_data; /** String data in 'modified utf-8'.*/ + // store everything here. + union + { + int32_t int_data; + int64_t long_data; + float float_data; + double double_data; + uint16_t index; + struct + { + /** + * Class reference: + * an index within the constant pool to a UTF-8 string containing + * the fully qualified class name (in internal format) + * Used for j_class, j_fieldref, j_methodref and j_interface_methodref + */ + uint16_t class_idx; + // used for j_fieldref, j_methodref and j_interface_methodref + uint16_t name_and_type_idx; + } ref_type; + struct + { + uint16_t name_index; + uint16_t descriptor_index; + } name_and_type; + }; + }; + + /** + * A helper class that represents the custom container used in Java class file for storage of constants + */ + class constant_pool + { + public: + /** + * Create a pool of constants + */ + constant_pool(){} + /** + * Load a java constant pool + */ + void load(util::membuffer & buf) + { + uint16_t length = 0; + buf.read_be(length); + length --; + uint16_t index = 1; + const constant * last_constant = nullptr; + while(length) + { + const constant & cnst = constant(buf); + constants.push_back(cnst); + last_constant = &constants[constants.size() - 1]; + if(last_constant->type == constant::j_double || last_constant->type == constant::j_long) + { + // push in a fake constant to preserve indexing + constants.push_back(constant(0)); + length-=2; + index+=2; + } + else + { + length--; + index++; + } + } + } + typedef std::vector container_type; + /** + * Access constants based on jar file index numbers (index of the first element is 1) + */ + java::constant & operator[](std::size_t constant_index) + { + if(constant_index == 0 || constant_index > constants.size()) + { + throw new classfile_exception(); + } + return constants[constant_index - 1]; + }; + container_type::const_iterator begin() const + { + return constants.begin(); + }; + container_type::const_iterator end() const + { + return constants.end(); + } + private: + container_type constants; + }; +} diff --git a/depends/classparser/src/errors.h b/depends/classparser/src/errors.h new file mode 100644 index 00000000..c02b07c8 --- /dev/null +++ b/depends/classparser/src/errors.h @@ -0,0 +1,6 @@ +#pragma once +#include +namespace java +{ + class classfile_exception : public std::exception {}; +} diff --git a/depends/classparser/src/javaendian.h b/depends/classparser/src/javaendian.h new file mode 100644 index 00000000..fa6207fe --- /dev/null +++ b/depends/classparser/src/javaendian.h @@ -0,0 +1,62 @@ +#pragma once +#include + +/** + * Swap bytes between big endian and local number representation + */ +namespace util +{ +#ifdef MULTIMC_BIG_ENDIAN +inline uint64_t bigswap(uint64_t x) +{ + return x; +}; +inline uint32_t bigswap(uint32_t x) +{ + return x; +}; +inline uint16_t bigswap(uint16_t x) +{ + return x; +}; +inline int64_t bigswap(int64_t x) +{ + return x; +}; +inline int32_t bigswap(int32_t x) +{ + return x; +}; +inline int16_t bigswap(int16_t x) +{ + return x; +}; +#else +inline uint64_t bigswap(uint64_t x) +{ + return (x>>56) | ((x<<40) & 0x00FF000000000000) | ((x<<24) & 0x0000FF0000000000) | ((x<<8) & 0x000000FF00000000) | + ((x>>8) & 0x00000000FF000000) | ((x>>24) & 0x0000000000FF0000) | ((x>>40) & 0x000000000000FF00) | (x<<56); +}; +inline uint32_t bigswap(uint32_t x) +{ + return (x>>24) | ((x<<8) & 0x00FF0000) | ((x>>8) & 0x0000FF00) | (x<<24); +}; +inline uint16_t bigswap(uint16_t x) +{ + return (x>>8) | (x<<8); +}; +inline int64_t bigswap(int64_t x) +{ + return (x>>56) | ((x<<40) & 0x00FF000000000000) | ((x<<24) & 0x0000FF0000000000) | ((x<<8) & 0x000000FF00000000) | + ((x>>8) & 0x00000000FF000000) | ((x>>24) & 0x0000000000FF0000) | ((x>>40) & 0x000000000000FF00) | (x<<56); +}; +inline int32_t bigswap(int32_t x) +{ + return (x>>24) | ((x<<8) & 0x00FF0000) | ((x>>8) & 0x0000FF00) | (x<<24); +}; +inline int16_t bigswap(int16_t x) +{ + return (x>>8) | (x<<8); +}; +#endif +} diff --git a/depends/classparser/src/javautils.cpp b/depends/classparser/src/javautils.cpp new file mode 100644 index 00000000..4a359031 --- /dev/null +++ b/depends/classparser/src/javautils.cpp @@ -0,0 +1,81 @@ +/* Copyright 2013 MultiMC Contributors + * + * Authors: Orochimarufan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "multimc_pragma.h" +#include "classfile.h" +#include "javautils.h" + +#include +#include + +namespace javautils +{ + +QString GetMinecraftJarVersion(QString jarName) +{ + QString version = MCVer_Unknown; + + // check if minecraft.jar exists + QFile jar(jarName); + if (!jar.exists()) + return version; + + // open minecraft.jar + QuaZip zip(&jar); + if (!zip.open(QuaZip::mdUnzip)) + return version; + + // open Minecraft.class + zip.setCurrentFile("net/minecraft/client/Minecraft.class", QuaZip::csSensitive); + QuaZipFile Minecraft(&zip); + if (!Minecraft.open(QuaZipFile::ReadOnly)) + return version; + + // read Minecraft.class + qint64 size = Minecraft.size(); + char *classfile = new char[size]; + Minecraft.read(classfile, size); + + // parse Minecraft.class + try { + char *temp = classfile; + java::classfile MinecraftClass(temp, size); + java::constant_pool constants = MinecraftClass.constants; + for(java::constant_pool::container_type::const_iterator iter=constants.begin(); + iter != constants.end(); iter++) + { + const java::constant & constant = *iter; + if (constant.type != java::constant::j_string_data) + continue; + const std::string & str = constant.str_data; + if (str.compare(0, 20, "Minecraft Minecraft ") == 0) + { + version = str.substr(20).data(); + break; + } + } + } catch(java::classfile_exception &) {} + + // clean up + delete[] classfile; + Minecraft.close(); + zip.close(); + jar.close(); + + return version; +} + +} diff --git a/depends/classparser/src/membuffer.h b/depends/classparser/src/membuffer.h new file mode 100644 index 00000000..2ea3a69b --- /dev/null +++ b/depends/classparser/src/membuffer.h @@ -0,0 +1,64 @@ +#pragma once +#include +#include +#include +#include +#include "javaendian.h" + +namespace util +{ + class membuffer + { + public: + membuffer(char * buffer, std::size_t size) + { + current = start = buffer; + end = start + size; + } + ~membuffer() + { + // maybe? possibly? left out to avoid confusion. for now. + //delete start; + } + /** + * Read some value. That's all ;) + */ + template + void read(T& val) + { + val = *(T *)current; + current += sizeof(T); + } + /** + * Read a big-endian number + * valid for 2-byte, 4-byte and 8-byte variables + */ + template + void read_be(T& val) + { + val = util::bigswap(*(T *)current); + current += sizeof(T); + } + /** + * Read a string in the format: + * 2B length (big endian, unsigned) + * length bytes data + */ + void read_jstr(std::string & str) + { + uint16_t length = 0; + read_be(length); + str.append(current,length); + current += length; + } + /** + * Skip N bytes + */ + void skip (std::size_t N) + { + current += N; + } + private: + char * start, *end, *current; + }; +} diff --git a/depends/groupview/CMakeLists.txt b/depends/groupview/CMakeLists.txt new file mode 100644 index 00000000..3fa2b044 --- /dev/null +++ b/depends/groupview/CMakeLists.txt @@ -0,0 +1,41 @@ +project(libGroupView) + +set(CMAKE_AUTOMOC ON) + +# Find Qt +find_package(Qt5Core REQUIRED) +find_package(Qt5Widgets REQUIRED) + +# Include Qt headers. +include_directories(${Qt5Base_INCLUDE_DIRS}) + +SET(LIBGROUPVIEW_HEADERS +include/groupview_config.h + +# Public headers +include/categorizedsortfilterproxymodel.h +include/categorizedview.h +include/categorydrawer.h + +# Private headers +src/categorizedsortfilterproxymodel_p.h +src/categorizedview_p.h +) + +SET(LIBGROUPVIEW_SOURCES +src/categorizedsortfilterproxymodel.cpp +src/categorizedview.cpp +src/categorydrawer.cpp +) + +# Set the include dir path. +SET(LIBGROUPVIEW_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include" PARENT_SCOPE) + +# Include self. +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) +include_directories(${CMAKE_BINARY_DIR}/include) + +add_definitions(-DLIBGROUPVIEW_LIBRARY) + +add_library(libGroupView SHARED ${LIBGROUPVIEW_SOURCES} ${LIBGROUPVIEW_HEADERS}) +qt5_use_modules(libGroupView Core Widgets) diff --git a/depends/groupview/include/categorizedsortfilterproxymodel.h b/depends/groupview/include/categorizedsortfilterproxymodel.h new file mode 100644 index 00000000..d90fb254 --- /dev/null +++ b/depends/groupview/include/categorizedsortfilterproxymodel.h @@ -0,0 +1,175 @@ +/* + * This file is part of the KDE project + * Copyright (C) 2007 Rafael Fernández López + * Copyright (C) 2007 John Tapsell + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef KCATEGORIZEDSORTFILTERPROXYMODEL_H +#define KCATEGORIZEDSORTFILTERPROXYMODEL_H + +#include + +#include + +class QItemSelection; + + +/** + * This class lets you categorize a view. It is meant to be used along with + * KCategorizedView class. + * + * In general terms all you need to do is to reimplement subSortLessThan() and + * compareCategories() methods. In order to make categorization work, you need + * to also call setCategorizedModel() class to enable it, since the categorization + * is disabled by default. + * + * @see KCategorizedView + * + * @author Rafael Fernández López + */ +class LIBGROUPVIEW_EXPORT KCategorizedSortFilterProxyModel + : public QSortFilterProxyModel +{ +public: + enum AdditionalRoles + { + // Note: use printf "0x%08X\n" $(($RANDOM*$RANDOM)) + // to define additional roles. + CategoryDisplayRole = 0x17CE990A, ///< This role is used for asking the category to a given index + + CategorySortRole = 0x27857E60 ///< This role is used for sorting categories. You can return a + ///< string or a long long value. Strings will be sorted alphabetically + ///< while long long will be sorted by their value. Please note that this + ///< value won't be shown on the view, is only for sorting purposes. What will + ///< be shown as "Category" on the view will be asked with the role + ///< CategoryDisplayRole. + }; + + KCategorizedSortFilterProxyModel ( QObject *parent = 0 ); + virtual ~KCategorizedSortFilterProxyModel(); + + /** + * Overridden from QSortFilterProxyModel. Sorts the source model using + * @p column for the given @p order. + */ + virtual void sort ( int column, Qt::SortOrder order = Qt::AscendingOrder ); + + /** + * @return whether the model is categorized or not. Disabled by default. + */ + bool isCategorizedModel() const; + + /** + * Enables or disables the categorization feature. + * + * @param categorizedModel whether to enable or disable the categorization feature. + */ + void setCategorizedModel ( bool categorizedModel ); + + /** + * @return the column being used for sorting. + */ + int sortColumn() const; + + /** + * @return the sort order being used for sorting. + */ + Qt::SortOrder sortOrder() const; + + /** + * Set if the sorting using CategorySortRole will use a natural comparison + * in the case that strings were returned. If enabled, QString::localeAwareCompare + * will be used for sorting. + * + * @param sortCategoriesByNaturalComparison whether to sort using a natural comparison or not. + */ + void setSortCategoriesByNaturalComparison ( bool sortCategoriesByNaturalComparison ); + + /** + * @return whether it is being used a natural comparison for sorting. Enabled by default. + */ + bool sortCategoriesByNaturalComparison() const; + +protected: + /** + * Overridden from QSortFilterProxyModel. If you are subclassing + * KCategorizedSortFilterProxyModel, you will probably not need to reimplement this + * method. + * + * It calls compareCategories() to sort by category. If the both items are in the + * same category (i.e. compareCategories returns 0), then subSortLessThan is called. + * + * @return Returns true if the item @p left is less than the item @p right when sorting. + * + * @warning You usually won't need to reimplement this method when subclassing + * from KCategorizedSortFilterProxyModel. + */ + virtual bool lessThan ( const QModelIndex &left, const QModelIndex &right ) const; + + /** + * This method has a similar purpose as lessThan() has on QSortFilterProxyModel. + * It is used for sorting items that are in the same category. + * + * @return Returns true if the item @p left is less than the item @p right when sorting. + */ + virtual bool subSortLessThan ( const QModelIndex &left, const QModelIndex &right ) const; + + /** + * This method compares the category of the @p left index with the category + * of the @p right index. + * + * Internally and if not reimplemented, this method will ask for @p left and + * @p right models for role CategorySortRole. In order to correctly sort + * categories, the data() metod of the model should return a qlonglong (or numeric) value, or + * a QString object. QString objects will be sorted with QString::localeAwareCompare if + * sortCategoriesByNaturalComparison() is true. + * + * @note Please have present that: + * QString(QChar(QChar::ObjectReplacementCharacter)) > + * QString(QChar(QChar::ReplacementCharacter)) > + * [ all possible strings ] > + * QString(); + * + * This means that QString() will be sorted the first one, while + * QString(QChar(QChar::ObjectReplacementCharacter)) and + * QString(QChar(QChar::ReplacementCharacter)) will be sorted in last + * position. + * + * @warning Please note that data() method of the model should return always + * information of the same type. If you return a QString for an index, + * you should return always QStrings for all indexes for role CategorySortRole + * in order to correctly sort categories. You can't mix by returning + * a QString for one index, and a qlonglong for other. + * + * @note If you need a more complex layout, you will have to reimplement this + * method. + * + * @return A negative value if the category of @p left should be placed before the + * category of @p right. 0 if @p left and @p right are on the same category, and + * a positive value if the category of @p left should be placed after the + * category of @p right. + */ + virtual int compareCategories ( const QModelIndex &left, const QModelIndex &right ) const; + +private: + class Private; + Private *const d; +}; + + +#endif // KCATEGORIZEDSORTFILTERPROXYMODEL_H diff --git a/depends/groupview/include/categorizedview.h b/depends/groupview/include/categorizedview.h new file mode 100644 index 00000000..81b1dbb1 --- /dev/null +++ b/depends/groupview/include/categorizedview.h @@ -0,0 +1,332 @@ +/** + * This file is part of the KDE project + * Copyright (C) 2007, 2009 Rafael Fernández López + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef KCATEGORIZEDVIEW_H +#define KCATEGORIZEDVIEW_H + +#include + +#include + +class KCategoryDrawer; + +/** + * @short Item view for listing items in a categorized fashion optionally + * + * KCategorizedView basically has the same functionality as QListView, only that it also lets you + * layout items in a way that they are categorized visually. + * + * For it to work you will need to set a KCategorizedSortFilterProxyModel and a KCategoryDrawer + * with methods setModel() and setCategoryDrawer() respectively. Also, the model will need to be + * flagged as categorized with KCategorizedSortFilterProxyModel::setCategorizedModel(true). + * + * The way it works (if categorization enabled): + * + * - When sorting, it does more things than QListView does. It will ask the model for the + * special role CategorySortRole (@see KCategorizedSortFilterProxyModel). This can return + * a QString or an int in order to tell the view the order of categories. In this sense, for + * instance, if we are sorting by name ascending, "A" would be before than "B". If we are + * sorting by size ascending, 512 bytes would be before 1024 bytes. This way categories are + * also sorted. + * + * - When the view has to paint, it will ask the model with the role CategoryDisplayRole + * (@see KCategorizedSortFilterProxyModel). It will for instance return "F" for "foo.pdf" if + * we are sorting by name ascending, or "Small" if a certain item has 100 bytes, for example. + * + * For drawing categories, KCategoryDrawer will be used. You can inherit this class to do your own + * drawing. + * + * @note All examples cited before talk about filesystems and such, but have present that this + * is a completely generic class, and it can be used for whatever your purpose is. For + * instance when talking about animals, you can separate them by "Mammal" and "Oviparous". In + * this very case, for example, the CategorySortRole and the CategoryDisplayRole could be the + * same ("Mammal" and "Oviparous"). + * + * @note There is a really performance boost if CategorySortRole returns an int instead of a QString. + * Have present that this role is asked (n * log n) times when sorting and compared. Comparing + * ints is always faster than comparing strings, whithout mattering how fast the string + * comparison is. Consider thinking of a way of returning ints instead of QStrings if your + * model can contain a high number of items. + * + * @warning Note that for really drawing items in blocks you will need some things to be done: + * - The model set to this view has to be (or inherit if you want to do special stuff + * in it) KCategorizedSortFilterProxyModel. + * - This model needs to be set setCategorizedModel to true. + * - Set a category drawer by calling setCategoryDrawer. + * + * @see KCategorizedSortFilterProxyModel, KCategoryDrawer + * + * @author Rafael Fernández López + */ +class LIBGROUPVIEW_EXPORT KCategorizedView + : public QListView +{ + Q_OBJECT + Q_PROPERTY ( int categorySpacing READ categorySpacing WRITE setCategorySpacing ) + Q_PROPERTY ( bool alternatingBlockColors READ alternatingBlockColors WRITE setAlternatingBlockColors ) + Q_PROPERTY ( bool collapsibleBlocks READ collapsibleBlocks WRITE setCollapsibleBlocks ) + +public: + KCategorizedView ( QWidget *parent = 0 ); + + ~KCategorizedView(); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void setModel ( QAbstractItemModel *model ); + + /** + * Calls to setGridSizeOwn(). + */ + void setGridSize ( const QSize &size ); + + /** + * @warning note that setGridSize is not virtual in the base class (QListView), so if you are + * calling to this method, make sure you have a KCategorizedView pointer around. This + * means that something like: + * @code + * QListView *lv = new KCategorizedView(); + * lv->setGridSize(mySize); + * @endcode + * + * will not call to the expected setGridSize method. Instead do something like this: + * + * @code + * QListView *lv; + * ... + * KCategorizedView *cv = qobject_cast(lv); + * if (cv) { + * cv->setGridSizeOwn(mySize); + * } else { + * lv->setGridSize(mySize); + * } + * @endcode + * + * @note this method will call to QListView::setGridSize among other operations. + * + * @since 4.4 + */ + void setGridSizeOwn ( const QSize &size ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual QRect visualRect ( const QModelIndex &index ) const; + + /** + * Returns the current category drawer. + */ + KCategoryDrawer *categoryDrawer() const; + + /** + * The category drawer that will be used for drawing categories. + */ + void setCategoryDrawer ( KCategoryDrawer *categoryDrawer ); + + /** + * @return Category spacing. The spacing between categories. + * + * @since 4.4 + */ + int categorySpacing() const; + + /** + * Stablishes the category spacing. This is the spacing between categories. + * + * @since 4.4 + */ + void setCategorySpacing ( int categorySpacing ); + + /** + * @return Whether blocks should be drawn with alternating colors. + * + * @since 4.4 + */ + bool alternatingBlockColors() const; + + /** + * Sets whether blocks should be drawn with alternating colors. + * + * @since 4.4 + */ + void setAlternatingBlockColors ( bool enable ); + + /** + * @return Whether blocks can be collapsed or not. + * + * @since 4.4 + */ + bool collapsibleBlocks() const; + + /** + * Sets whether blocks can be collapsed or not. + * + * @since 4.4 + */ + void setCollapsibleBlocks ( bool enable ); + + /** + * @return Block of indexes that are into @p category. + * + * @since 4.5 + */ + QModelIndexList block ( const QString &category ); + + /** + * @return Block of indexes that are represented by @p representative. + * + * @since 4.5 + */ + QModelIndexList block ( const QModelIndex &representative ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual QModelIndex indexAt ( const QPoint &point ) const; + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void reset(); + + /** + * Signify that all item delegates size hints return the same fixed size + */ + void setUniformItemWidths(bool enable); + + /** + * Do all item delegate size hints return the same fixed size? + */ + bool uniformItemWidths() const; + +protected: + /** + * Reimplemented from QWidget. + */ + virtual void paintEvent ( QPaintEvent *event ); + + /** + * Reimplemented from QWidget. + */ + virtual void resizeEvent ( QResizeEvent *event ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void setSelection ( const QRect &rect, + QItemSelectionModel::SelectionFlags flags ); + + /** + * Reimplemented from QWidget. + */ + virtual void mouseMoveEvent ( QMouseEvent *event ); + + /** + * Reimplemented from QWidget. + */ + virtual void mousePressEvent ( QMouseEvent *event ); + + /** + * Reimplemented from QWidget. + */ + virtual void mouseReleaseEvent ( QMouseEvent *event ); + + /** + * Reimplemented from QWidget. + */ + virtual void leaveEvent ( QEvent *event ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void startDrag ( Qt::DropActions supportedActions ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void dragMoveEvent ( QDragMoveEvent *event ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void dragEnterEvent ( QDragEnterEvent *event ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void dragLeaveEvent ( QDragLeaveEvent *event ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void dropEvent ( QDropEvent *event ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual QModelIndex moveCursor ( CursorAction cursorAction, + Qt::KeyboardModifiers modifiers ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void rowsAboutToBeRemoved ( const QModelIndex &parent, + int start, + int end ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void updateGeometries(); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void currentChanged ( const QModelIndex ¤t, + const QModelIndex &previous ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void dataChanged ( const QModelIndex &topLeft, + const QModelIndex &bottomRight ); + + /** + * Reimplemented from QAbstractItemView. + */ + virtual void rowsInserted ( const QModelIndex &parent, + int start, + int end ); + +protected Q_SLOTS: + /** + * @internal + * Reposition items as needed. + */ + virtual void slotLayoutChanged(); + virtual void slotCollapseOrExpandClicked ( QModelIndex ); + +private: + class Private; + Private *const d; +}; + +#endif // KCATEGORIZEDVIEW_H diff --git a/depends/groupview/include/categorydrawer.h b/depends/groupview/include/categorydrawer.h new file mode 100644 index 00000000..f37422ec --- /dev/null +++ b/depends/groupview/include/categorydrawer.h @@ -0,0 +1,179 @@ +/** + * This file is part of the KDE project + * Copyright (C) 2007, 2009 Rafael Fernández López + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public License + * along with this library; see the file COPYING.LIB. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef KCATEGORYDRAWER_H +#define KCATEGORYDRAWER_H + +#include + +#include +#include + +class QPainter; +class QModelIndex; +class QStyleOption; +class KCategorizedView; + + +/** + * @since 4.5 + */ +class LIBGROUPVIEW_EXPORT KCategoryDrawer + : public QObject +{ + friend class KCategorizedView; + Q_OBJECT + + +public: + KCategoryDrawer ( KCategorizedView *view ); + virtual ~KCategoryDrawer(); + + /** + * @return The view this category drawer is associated with. + */ + KCategorizedView *view() const; + + /** + * This method purpose is to draw a category represented by the given + * @param index with the given @param sortRole sorting role + * + * @note This method will be called one time per category, always with the + * first element in that category + */ + virtual void drawCategory ( const QModelIndex &index, + int sortRole, + const QStyleOption &option, + QPainter *painter ) const; + + /** + * @return The category height for the category representated by index @p index with + * style options @p option. + */ + virtual int categoryHeight ( const QModelIndex &index, const QStyleOption &option ) const; + + //TODO KDE5: make virtual as leftMargin + /** + * @note 0 by default + * + * @since 4.4 + */ + int leftMargin() const; + + /** + * @note call to this method on the KCategoryDrawer constructor to set the left margin + * + * @since 4.4 + */ + void setLeftMargin ( int leftMargin ); + + //TODO KDE5: make virtual as rightMargin + /** + * @note 0 by default + * + * @since 4.4 + */ + int rightMargin() const; + + /** + * @note call to this method on the KCategoryDrawer constructor to set the right margin + * + * @since 4.4 + */ + void setRightMargin ( int rightMargin ); + + KCategoryDrawer &operator= ( const KCategoryDrawer &cd ); +protected: + /** + * Method called when the mouse button has been pressed. + * + * @param index The representative index of the block of items. + * @param blockRect The rect occupied by the block of items. + * @param event The mouse event. + * + * @warning You explicitly have to determine whether the event has been accepted or not. You + * have to call event->accept() or event->ignore() at all possible case branches in + * your code. + */ + virtual void mouseButtonPressed ( const QModelIndex &index, const QRect &blockRect, QMouseEvent *event ); + + /** + * Method called when the mouse button has been released. + * + * @param index The representative index of the block of items. + * @param blockRect The rect occupied by the block of items. + * @param event The mouse event. + * + * @warning You explicitly have to determine whether the event has been accepted or not. You + * have to call event->accept() or event->ignore() at all possible case branches in + * your code. + */ + virtual void mouseButtonReleased ( const QModelIndex &index, const QRect &blockRect, QMouseEvent *event ); + + /** + * Method called when the mouse has been moved. + * + * @param index The representative index of the block of items. + * @param blockRect The rect occupied by the block of items. + * @param event The mouse event. + */ + virtual void mouseMoved ( const QModelIndex &index, const QRect &blockRect, QMouseEvent *event ); + + /** + * Method called when the mous